Remove Underline on Uibutton in iOS 7

Remove underline on UIButton in iOS 7

Check below code :

NSMutableAttributedString *attrStr = [[yourBtnHere attributedTitleForState:UIControlStateNormal] mutableCopy];//or whatever the state you want
[attrStr enumerateAttributesInRange:NSMakeRange(0, [attrStr length])
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop)
{
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
[mutableAttributes removeObjectForKey:NSUnderlineStyleAttributeName];
[attrStr setAttributes:mutableAttributes range:range];
}];

• With the inspector/IB:
Select your UIButton.

Show the Attributes Inspector.

The Textsettings should be in Attributed. Select the text, click on the fond item remove the Underlining setting it at none.

Sample Image

Underline button text in Swift

Here you go, just tested it. (works in xCode 7 Beta at least)

@IBOutlet weak var yourButton: UIButton!

var attrs = [
NSFontAttributeName : UIFont.systemFontOfSize(19.0),
NSForegroundColorAttributeName : UIColor.redColor(),
NSUnderlineStyleAttributeName : 1]

var attributedString = NSMutableAttributedString(string:"")

override func viewDidLoad() {
super.viewDidLoad()

let buttonTitleStr = NSMutableAttributedString(string:"My Button", attributes:attrs)
attributedString.appendAttributedString(buttonTitleStr)
yourButton.setAttributedTitle(attributedString, forState: .Normal)
}

Underlining text in UIButton

UIUnderlinedButton.h

@interface UIUnderlinedButton : UIButton {

}

+ (UIUnderlinedButton*) underlinedButton;
@end

UIUnderlinedButton.m

@implementation UIUnderlinedButton

+ (UIUnderlinedButton*) underlinedButton {
UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
return [button autorelease];
}

- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;

// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;

CGContextRef contextRef = UIGraphicsGetCurrentContext();

// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);

CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);

CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);

CGContextClosePath(contextRef);

CGContextDrawPath(contextRef, kCGPathStroke);
}

@end

Swift: Remove a UIButton attributedTitle

You need to set the AttributedTitle to nil before setting using setTitle for your button, Its working i have checked it.

like this

myButton.setAttributedTitle(nil, for: .normal)
myButton.setTitle(myRegularText, for: .normal)

How Can I Underline the Button Text of UIButton?

For this you can subclass UILabel and overwrite its -drawRect method and then use your own UILabel and add a UIButton over it of custom type.

Make your drawRect method in UILabel as

- (void)drawRect:(CGRect)rect 
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f);

CGContextSetLineWidth(context, 1.0f);

CGContextMoveToPoint(context, 0, self.bounds.size.height - 1);
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - 1);

CGContextStrokePath(context);

[super drawRect:rect];

}

Remove Bold, Italic, and Underline options from the UIWebView in iOS

I've investigated a lot to solve this problem and finally found a solution. I first though overriding - (BOOL)canPerformAction:(SEL)action withSender:(id)sender in the UIViewController containing the UIWebView would be sufficient to prevent text style options to show up. But unfortunately, it wasn't as easy as it seems.

The main reason for this is that we have to override the canPerformAction of the main first responder. Calling [[[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)] from the controller informs us that UIWebBrowserView is the actual main first responder. We would want to subclass UIWebBrowserView, but since it's a private class, we might have our app rejected by Apple during review process. This answer from @Shayan RC suggested to perform a method swizzling to allow kind of overriding this method without subclassing UIWebBrowserView (and thus prevent from App Store rejection).


SOLUTION :

The idea is to add a new method that would replace canPerformAction. I've created an array with all methods signatures that we want to keep in the Menu. To remove styling options, we just have to not add @"_showTextStyleOptions:" to this array. Add every other method signature that you want to show (I've added a NSLog of the signatures, so that you can pick the ones you want).

- (BOOL) mightPerformAction:(SEL)action withSender:(id)sender {
NSLog(@"action : %@", NSStringFromSelector(action));

NSArray<NSString*> *selectorsToKeep = @[@"cut:", @"copy:", @"select:", @"selectAll:", @"_lookup:"]; //add in this array every action you want to keep
if ([selectorsToKeep containsObject:NSStringFromSelector(action)]) {
return YES;
}

return NO;
}

Now we can perform method swizzling to call previous method instead of canPerformAction, with following method (from the answer of @Shayan RC). This will require to add #import <objc/runtime.h>.

- (void) replaceUIWebBrowserView: (UIView *)view {
//Iterate through subviews recursively looking for UIWebBrowserView
for (UIView *sub in view.subviews) {
[self replaceUIWebBrowserView:sub];
if ([NSStringFromClass([sub class]) isEqualToString:@"UIWebBrowserView"]) {
Class class = sub.class;

SEL originalSelector = @selector(canPerformAction:withSender:);
SEL swizzledSelector = @selector(mightPerformAction:withSender:);

Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self.class, swizzledSelector);

//add the method mightPerformAction:withSender: to UIWebBrowserView
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
//replace canPerformAction:withSender: with mightPerformAction:withSender:
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
}
}

Finally, call previous method in viewDidLoad like so : [self replaceUIWebBrowserView:_webView].

It could seems hard with method swizzling, but it allows you to keep the code in your view controller. Please tell me if you encounter any difficulties in implementing previous code.

NOTE : This behavior is much more easy to implement with WKWebView than with UIWebView, and UIWebView is deprecated, you should really consider switching to WKWebView.

How to display Underlined Text in a Button on iOS?

Another alternative is to set an image for the button. Create an image that displays the appropriate text and the button will display that instead of text.

I agree with Zack that underlining text is redundant and violates the interface grammar. You underline text for links in the first place to indicate that they have behavior like buttons. If the text is already in a button, there is no need to underline it to indicate it behaves like a button because the user already expects an action in response to clicking it.



Related Topics



Leave a reply



Submit