how to set attributes in NSAttributedString in iOS?
I’m trying to set the infamous NSFontAttributeName property of an NSAttributedString in iOS but it just doesn’t seem to work:
- first off, none of the NS constants seem defined for iOS
- I read somewhere that I could instead work around it by passing
the CoreText consts instead. Fine… but still, The attribute
expects an NSFont and I’m stuck with UIFont or CTFontRef, neither of
which seems to work:
this doesn’t work:
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:16].fontName, [UIFont boldSystemFontOfSize:16].pointSize, NULL); [myAttString addAttribute:(NSString*)kCTFontNameAttribute value:(id)ctFont range:NSMakeRange(0, myAttString.length-1)];
this doesn’t work:
[myAttString addAttribute:(NSString*)kCTFontNameAttribute value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, myAttString.length-1)];
Is there anyway to make this work?
- NSAttributedString from HTML in the background thread
- How to make a clickable link in an NSAttributedString for a
- NSAttributedString initWithData and NSHTMLTextDocumentType crash if not on main thread
- replace entire text string in NSAttributedString without modifying other attributes
- create an attributed string out of plain (Android formated) text in swift for iOS
- NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)
3 Solutions Collect From Internet About “how to set attributes in NSAttributedString in iOS?”
I found it!
basically, turns out the string constant for the dictionary key I should been using is kCTFontAttributeName
This whole thing is a show…
The NS constants and full attributedString support will be there. Not yet in iOS5 though.
The CoreText constants do work and CTFontRef is the way I use it as well. The first block of your code should work. Can you verify your other bits of code that the problem ain’t elsewhere.
do this way:
let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center let attributes = [NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : UIFont.systemFont(ofSize: 24.0), NSForegroundColorAttributeName : UIColor.blue, ] let attrString = NSAttributedString(string: "Stop\nall Dance", attributes: attributes)
- What do these symbols (-fno-objc-arc) mean?
- IPhone Connectivity Testing: How do I force it to lose connection?
- refreshPreferences HangTracerEnabled / HangTracerDuration messages in iOS 11 + Xcode 9
- Error message '_BSMachError: (os/kern) invalid capability (20)'
- Finding the difference in time between two NSDates Swift
- iOS How I can load multiple in-app items
- Format currency with UITextField
- Dealing with different iOS device resolutions in SpriteKit
- UIPickerView as inputView of UITextField
- position: fixed doesn't work on iPad and iPhone
- Delete an asset (picture or video) from IPhone in IOS
- Play YouTube videos with MPMoviePlayerController instead of UIWebView
- Obj-C, Instance variable used while 'self' is not set to the result of ''
- UITableView set a web view and a button outside the web view as background
- sort array of anyobject Swift 3