nsattributedstring

Why does the initial call to NSAttributedString with an HTML string take over 100 times longer than subsequent calls?

寵の児 提交于 2019-11-27 01:06:48
问题 I had a need to display HTML text inside my iOS app. I have decided I will use the built-in method on NSAttributedString , initWithData:options:documentAttributes:error:. The actual parsing works excellently, however, I seem to have come across a very odd bug, that only seems to manifest itself if I have the debugger attached. The first time that this method is called, it takes barely under 1 second to run on my iPhone 5S running iOS 7.0.4, and about 1.5 seconds on an iPod Touch 5th

Attributed Text Center Alignment

醉酒当歌 提交于 2019-11-27 00:49:35
问题 I have tried everything but cannot seem to center this text. Can someone please tell me where the error is. NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; label.attributedText = [[NSAttributedString alloc] initWithString:cell.EventTitle.text attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName : @0,NSFontAttributeName : [UIFont

Example of NSAttributedString with two different font sizes?

随声附和 提交于 2019-11-27 00:43:45
NSAttributedString is just really impenetrable to me. I want to set a UILabel to have text of different sizes, and I gather NSAttributedString is the way to go, but I can't get anywhere with the documentation on this. I would love it if someone could help me with a concrete example. For instance, let's say the text I wanted was: (in small letters:) "Presenting The Great..." (in huge letters:) "HULK HOGAN!" Can somebody show me how to do that? Or even, a reference that's plain and simple where I could learn for myself? I swear I've tried to understand this through the documentation, and even

How can I concatenate NSAttributedStrings?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 23:58:50
问题 I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is there any way to concatenate attributedString to another attributedString? 回答1: I'd recommend you use a single mutable attributed string a @Linuxios suggested, and here's another example of that: NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init]; NSString *plainString = // ...

iOS NSAttributedString on UIButton

落花浮王杯 提交于 2019-11-26 22:35:33
问题 I'm using iOS 6 , so attributed strings should be easy to use, right? Well... not so much. What I want to do: Using a custom subclass of UIButton (it doesn't do anything custom to titleLabel ), I'd like to have a multi-line, attributed title that is: All caps (I realize that's not part of the attributes) on the first line Bolded on the first line Underlined on the first line "Normal" weight on the second line Non-underlined on the second line Centered on both lines I've been able to get #'s 1

Replace substring of NSAttributedString with another NSAttributedString

蓝咒 提交于 2019-11-26 22:17:18
问题 I want to replace a substring (e.g. @"replace" ) of an NSAttributedString with another NSAttributedString . I am looking for an equivalent method to NSString 's stringByReplacingOccurrencesOfString:withString: for NSAttributedString . 回答1: Convert your attributed string into an instance of NSMutableAttributedString . The mutable attributed string has a mutableString property. According to the documentation: "The receiver tracks changes to this string and keeps its attribute mappings up to

Color attribute is ignored in NSAttributedString with NSLinkAttributeName

荒凉一梦 提交于 2019-11-26 21:47:53
问题 In an NSAttributedString , a range of letters has a link attribute and a custom color attribute. In Xcode 7 with Swift 2, it works: In Xcode 8 with Swift 3, the custom attributed color for the link is always ignored (it should be orange in the screenshot). Here's the code for testing. Swift 2, Xcode 7: import Cocoa import XCPlayground let text = "Hey @user!" let attr = NSMutableAttributedString(string: text) let range = NSRange(location: 4, length: 5) attr.addAttribute

boundingRectWithSize for NSAttributedString returning wrong size

房东的猫 提交于 2019-11-26 21:09:29
I am trying to get the rect for an attributed string, but the boundingRectWithSize call is not respecting the size I pass in and is returning a rect with a single line height as opposed to a large height (it is a long string). I have experimented by passing in a very large value for the height and also 0 as in the code below, but the rect returned is always the same. CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(300,0.0) options:NSStringDrawingUsesDeviceMetrics context:nil]; Is this broken, or do I need to do something else to have it returned a rect for wrapped text?

NSAttributedString, change the font overall BUT keep all other attributes?

喜你入骨 提交于 2019-11-26 20:46:24
Say I have an NSMutableAttributedString . The string has a varied mix of formatting throughout: Here is an example: This string is hell to change in iOS , it really sucks . However, the font per se is not the font you want. I want to: for each and every character, change that character to a specific font (say, Avenir) but, for each character, keep each of the other attributions (bold, italic, colors, etc etc) which was previously in place on that character. How the hell do you do this? Note: if you trivially add an attribute "Avenir" over the whole range: it simply deletes all the other

Is it possible to change color of single word in UITextView and UITextField

为君一笑 提交于 2019-11-26 20:30:20
Is it possible to change color of single word in UITextView and UITextField ? If i have typed a word with a symbol infront (eg: @word) , can it's color be changed ? Yes you need to use NSAttributedString for that, find the RunningAppHere . Scan through the word and find the range of your word and change its color. EDIT: - (IBAction)colorWord:(id)sender { NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text.text]; NSArray *words=[self.text.text componentsSeparatedByString:@" "]; for (NSString *word in words) { if ([word hasPrefix:@"@"]) { NSRange range