nsattributedstring

CoreText. How Do I Calculate the Bounding Box of an Attributed String?

穿精又带淫゛_ 提交于 2019-11-30 03:40:25
In CoreText it is easy ask: "for a given rectangle how much of this attributed string will fit?". CTFrameGetVisibleStringRange(rect).length Will return where in the string the next run of text should begin. My question is: "given an attributed string and a width, what rect height do I need to completely bound the attributed string?". Does the CoreText framework provide tools to do this? Thanks, Doug Joshua What you need is CTFramesetterSuggestFrameSizeWithConstraints() , you can use it like so: CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge

How to stop a time UILabel from resizing at every time increment?

前提是你 提交于 2019-11-30 02:40:41
问题 I have a stopwatch feature in my app that uses a centered attributed UILabel with a proportionally spaced font to render time. At every time increment the width of the label changes, creating a bouncing effect that looks especially bad at fast speeds. Here is an example. How can I fix this? iOS 9 UPDATE It is now a one-liner: UIFont.monospacedDigitSystemFontOfSize(17, weight: UIFontWeightRegular) Also, last time I tried, the solution below did not work for iOS 9. Wasted quite a bit of time

How to bold some words in my UITextView using NSMutableAttributedString?

半城伤御伤魂 提交于 2019-11-30 01:39:01
问题 I have a UITextView and there are certain words I'm casting with NSString stringWithFormat that I'd like to be bolded. I have looked around Stack Overflow and tried to follow the the postings but I guess I'm not understanding it. Here's what I've been playing around with: NSRange boldedRange = NSMakeRange(0, 4); NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName]; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self

NSAttributedString highlight/background color shows between lines (ugly)

点点圈 提交于 2019-11-30 01:37:05
问题 I'm trying to nicely display paragraphs of highlighted in a NSTextView. Right now, I'm doing this by creating a NSAttributedString with a background color. Here's some simplified code: NSDictionary *attributes = @{NSBackgroundColorAttributeName:NSColor.greenColor}; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Here is a single line of text with single spacing" attributes:attributes]; [textView.textStorage setAttributedString:attrString]; This approach basically

Center two fonts with different different sizes vertically in an NSAttributedString

て烟熏妆下的殇ゞ 提交于 2019-11-30 01:10:43
I use NSAttributedString to generate a string with two different sizes. By default, its bottom alignment looks like this: But I want to center it vertically, like this: To be clear, this is a single attributed string, not two or more. This is a simplified example to describe my question, what I'd actually like to do is more complex. I'd say the easiest thing to do is just manipulate the NSBaselineOffsetAttributeName attribute for the text in question: NSBaselineOffsetAttributeName The value of this attribute is an NSNumber object containing a floating point value indicating the character’s

In a UILabel, is it possible to force a line NOT to break in a certain place

点点圈 提交于 2019-11-30 01:08:31
问题 I have a UILabel that is supposed to be two lines long. The text is localized into French and English. I'm setting the attributedText property of the label. The text is constructed from three appended strings, say textFragment1, textFragment2 and textFragment3. The middle string is actually an image created with an NSTextAttachment. I want to make sure that textFragment2 and textFragment3 are on the same line. The first string may or may not wrap depending on how long it is. The problem is

Change attributes of substrings in a NSAttributedString

二次信任 提交于 2019-11-29 22:31:00
This question may be a duplicate of this one . But the answers don't work for me and I want to be more specific. I have a NSString , but I need a NS(Mutable)AttributedString and some of the words in this string should be given a different color. I tried this: NSString *text = @"This is the text and i want to replace something"; NSDictionary *attributes = @ {NSForegroundColorAttributeName : [UIColor redColor]}; NSMutableAttributedString *subString = [[NSMutableAttributedString alloc] initWithString:@"AND" attributes:attributes]; NSMutableAttributedString *newText = [[NSMutableAttributedString

Replace UITextViews text with attributed string

情到浓时终转凉″ 提交于 2019-11-29 21:54:29
I have a UITextView and when the user is entering text into it, I want to format the text on the fly. Something like syntax highlighting... For that I'd like to use UITextView ... Everything works fine expect one problem: I take the text from the text view and make an NSAttributedString from of it. I make some edits to this attributed string and set it back as the textView.attributedText . This happens everytime the user types. So I have to remember the selectedTextRange before the edit to the attributedText and set it back afterwards so that the user can continue typing at the place he was

NSParagraphStyle line spacing ignored

元气小坏坏 提交于 2019-11-29 21:00:24
A simple test that is failed: Make a new project with just one subview (UITextView) and put the following in: - (void)viewDidLoad { [super viewDidLoad]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple = 50.f; paragraphStyle.lineSpacing = 100.f; paragraphStyle.minimumLineHeight = 200.f; paragraphStyle.maximumLineHeight = 500.f; UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:24.f]; self.textView.attributedText = [[NSAttributedString alloc] initWithString: @"This is a test.\n Will I pass?" attributes: @

Core Text - NSAttributedString line height done right?

萝らか妹 提交于 2019-11-29 20:25:41
I'm completely in the dark with Core Text's line spacing. I'm using NSAttributedString and I specify the following attributes on it: - kCTFontAttributeName - kCTParagraphStyleAttributeName From this the CTFrameSetter gets created and drawn to context. In the paragraph style attribute I'd like to specify the height of the lines. When I use kCTParagraphStyleSpecifierLineHeightMultiple each line receives padding at the top of the text, instead of the text being displayed in the middle of this height. When I use kCTParagraphStyleSpecifierLineSpacing a padding is added to the bottom of the text.