nsattributedstring

Add NSUnderlineStyle.PatternDash to NSAttributedString in Swift?

混江龙づ霸主 提交于 2019-12-21 06:59:03
问题 I'm trying to add an underline to some text in my Swift app. This is the code I have currently: let text = NSMutableAttributedString(string: self.currentHome.name) let attrs = [NSUnderlineStyleAttributeName:NSUnderlineStyle.PatternDash] text.addAttributes(attrs, range: NSMakeRange(0, text.length)) homeLabel.attributedText = text But I get this error on the text.addAttributes line: NSString is not identical to NSObject How can I add an attribute contained in an enum to an

Rainbow text in TextView, iOS and OSX

北慕城南 提交于 2019-12-21 06:35:01
问题 I'm trying to add some different text colors to my app to be fused to an image. I've got alot of input that my users would like a rainbow text color and repeat. So for instance the word: stackoverflow would look like this: s=red t=orange a=yellow c=green k=blue o=purple v=pink e=red r=orange f=yellow l=green o=blue w=purple I can't even begin to think how I can do this in one single UITextView Does anyone know how I could achieve this as the user is typing? Tips? Example? I didn't see any

Objective C - How to create rtf from NSAttributedString

你。 提交于 2019-12-21 05:14:26
问题 I can convert from rtf string to attributed string using following: NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil]; Now how can i convert back from attributedString to rtf string? 回答1: You want to use -dataFromRange:documentAttributes:error: NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"YOLO" attributes:nil]; NSData *data = [str

Animated Gif inside UITextView

雨燕双飞 提交于 2019-12-21 05:08:28
问题 I had a look at this question and it didn't work. I am also trying to avoid this sort of solution. In the end I am willing to go with a CADisplayLink and change each frame manually. Still, is there a simpler way to do this? 回答1: An animated gif if just a collection of images which are displayed in sequence at a timed interval. You can do this with a UIImageView like this: imageView.animationImages = [UIImage(named: "uploadFrame0")!, UIImage(named: "uploadFrame1")!, UIImage(named:

NSAttributedString Color Test

大城市里の小女人 提交于 2019-12-21 04:50:38
问题 What is the correct way to compare, or test, for a particular color attribute of an NSAttributed string? As an example, I'd like to know if the text selection has red text. I have tried several approaches, as you can see below, but none of them ever results in a match. I see the text turn red on screen, and logging the attribute returns: UIDeviceRGBColorSpace 1 0 0 1 - (BOOL)isRedWithAttributes:(NSDictionary *)attributes { BOOL isRedWithAttributes = NO; if (attributes != nil) { // if (

how to change characters case to Upper in NSAttributedString

天大地大妈咪最大 提交于 2019-12-21 04:14:15
问题 I want to convert NSAttributedString containing RTFD to uppercase without losing attributes of existing characters and graphics. Thanks, 回答1: EDIT: @fluidsonic Is correct that the original code is incorrect. Below is an updated version in Swift, that replaces the text in each attribute range with an uppercased version of the string in that range. extension NSAttributedString { func uppercased() -> NSAttributedString { let result = NSMutableAttributedString(attributedString: self) result

Drawing colored text in UIView's -drawRect: method

人走茶凉 提交于 2019-12-20 17:42:26
问题 I am trying to draw colored text in my UIView subclass. Right now I am using the Single View app template (for testing). There are no modifications except the drawRect: method. The text is drawn but it is always black no matter what I set the color to. - (void)drawRect:(CGRect)rect { UIFont* font = [UIFont fontWithName:@"Arial" size:72]; UIColor* textColor = [UIColor redColor]; NSDictionary* stringAttrs = @{ UITextAttributeFont : font, UITextAttributeTextColor : textColor };

Drawing colored text in UIView's -drawRect: method

£可爱£侵袭症+ 提交于 2019-12-20 17:39:59
问题 I am trying to draw colored text in my UIView subclass. Right now I am using the Single View app template (for testing). There are no modifications except the drawRect: method. The text is drawn but it is always black no matter what I set the color to. - (void)drawRect:(CGRect)rect { UIFont* font = [UIFont fontWithName:@"Arial" size:72]; UIColor* textColor = [UIColor redColor]; NSDictionary* stringAttrs = @{ UITextAttributeFont : font, UITextAttributeTextColor : textColor };

UITextView with custom font not working when text set to “attributed”

青春壹個敷衍的年華 提交于 2019-12-20 12:28:23
问题 I have a UITextView with some text that came from a .rtf (pasted directly onto Xcode) The context contain only one custom font (Futura Book BT 11.0) If I set the "text(attributed)" property to "plain"= The custom font appear properly from the storyboard and from the app If I set the "text" property to "attributed"=. The custom font appear properly from the storyboard BUT not from the app. As my goal was to have a text with multiple font working, how to have the attributed property to work

How to get NSRange(s) for a substring in NSString? [duplicate]

浪子不回头ぞ 提交于 2019-12-20 10:57:24
问题 This question already has answers here : How to get all NSRange of a particular character in a NSString? (4 answers) Closed 6 years ago . NSString *str = @" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot..."; How can I loop through this NSString and get NSRange for each occurrence of "California", I want the NSRange because I would like to change it's color in the NSAttributed string. NSRange range = NSMakeRange(0,