Skip to content

Instantly share code, notes, and snippets.

@Darkyenus
Created August 31, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darkyenus/ecc129e6ec065304ceccf9bae201ba8f to your computer and use it in GitHub Desktop.
Save Darkyenus/ecc129e6ec065304ceccf9bae201ba8f to your computer and use it in GitHub Desktop.
Multi-OS-Engine Underline on NSAttributedString
protected static void makeTitleUnderlined(UIButton button, String title) {
final NSMutableAttributedString underlinedTitle = NSMutableAttributedString.alloc().initWithString(title);
NSMutableAttributedStringFix.makeUnderlined(underlinedTitle, NSMakeRange(0, underlinedTitle.length()));
button.setAttributedTitleForState(underlinedTitle, UIControlState.Normal);
}
/**
* Binding to the ObjC part of the workaround
*/
@Runtime(ObjCRuntime.class)
@ObjCCategory(NSMutableAttributedString.class)
private static class NSMutableAttributedStringFix extends NSMutableAttributedString {
static {
NatJ.register();
}
protected NSMutableAttributedStringFix(Pointer peer) {
super(peer);
}
@Selector("makeUnderlined:")
public static native void makeUnderlined(NSMutableAttributedString self, @ByValue NSRange range);
}
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@implementation NSMutableAttributedString (NSMutableAttributedStringFix)
- (void)makeUnderlined:(NSRange)range {
[self addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) range:range];
}
@end
@Darkyenus
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment