nsattributedstring

How to set color of templated image in NSTextAttachment

大兔子大兔子 提交于 2019-12-04 15:26:46
问题 How can I set the color of a templated image that is an attachment on an attributed string? Background: I've got a UILabel and I'm setting its attributedText to an NSAttributedString. The NSAttributedString includes an NSTextAttachment with a small image. Now I want to make my image color match the text color and I can't figure out how to make it work. I would normally expect to color the image by setting its rendering mode to UIImageRenderingModeAlwaysTemplate and then setting the tintColor

NSAttributedString end of first line indent

这一生的挚爱 提交于 2019-12-04 13:43:58
问题 I want to have the first line in an NSAttributedString for a UITextView indented from the right side on the first line. So the firstLineHeadIndent in NSParagraphStyle will indent the first line from the left. I want to do the same thing but from the right in my UITextView . Here's a screenshot of how I want the text to wrap. 回答1: The Setting Text Margins article from the Text System User Interface Layer Programming Guide has this figure: As you can see, there's no built-in mechanism to have a

Prevent line break in a NSAttributedString

我们两清 提交于 2019-12-04 12:34:34
I think it's a common issue when you have a set of words where you don't want a break line. Sometimes the character between those words is a space or a hyphen, etc. In my case it's a point :) This is my text 50.0/80.0 At the end I did it using the size label and measuring how much space I need for that string in particular: UIFont *fontAwardNumber = [UIFont fontWithName:@"DIN-Bold" size:20]; NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init]; CGSize labelSize = (CGSize){customCell.awardValueLabel.bounds.size.width, FLT_MAX}; CGRect rectNeededForAwardNumber = [awardNumber

create an attributed string out of plain (Android formatted) text in swift for iOS

痞子三分冷 提交于 2019-12-04 11:30:02
问题 I am reading strings out of a Localizable.strings which contains something like that which is basically what you have in an strings.xml of an Android app "testShort" = "A <b>short</b>\ntest with another<b>bold text</b>"; The bold and and line feed are the only two formatting attributes I have in my texts. I am trying to develop a method like this for days now without success: func ConvertText(inputText: String) -> NSAttributedString { // here comes the conversion to a representation with

Difference Between EffectiveRange and LongestEffectiveRange

泄露秘密 提交于 2019-12-04 09:53:50
In the NSAttributedString class, there are functions that get the values of attributes for certain indices and ranges but I'm unsure of the difference between the selectors attributesAtIndex:effectiveRange and attributesAtIndex:longestEffectiveRange:inRange: and when would I use one instead of the other? Thanks in advance for any clarification Let's say you have this kind of attributes: Range [0,2]: Black Background Color Range [0,2]: Bold Font Range [2,4]: Black Background Color Range [2,4]: Italic Font If you log this NSAttributedString , its attributes will be separated in two: Range [0,2]:

Add a click event to some text in ios NSString

无人久伴 提交于 2019-12-04 09:40:30
I have the following code and want to make parts of my text be clickable and call another UIViewController (not a website). NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; NSInteger length = str.length; [str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)]; The NSMutableAttributedString gets set to a UILabel like so: label.attributedText = str; Whats the best way to do this? I can't seem to find a great answer. An example of what I want is suppose I have a UILabel like so

Why do hyperlinks sometimes not show in an NSTextField with an NSAttributedString?

隐身守侯 提交于 2019-12-04 09:02:51
The text I use in an NSTextField is loaded from a file as follows. NSString *path = [[NSBundle mainBundle] pathForResource:@"Credits" ofType:@"rtf"]; NSAttributedString *as = [[NSAttributedString alloc] initWithPath:path documentAttributes:NULL]; [creditsLabel setAttributedStringValue:as]; [creditsLabel becomeFirstResponder]; The hyperlinks in the window don't render in blue underline unless I first click somewhere on the NSTextField, as per the two screenshots. How can I make these hyperlinks always look like hyperlinks? Here's the RTF: {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250 {

HTML from NSAttributedString

China☆狼群 提交于 2019-12-04 08:46:12
问题 Rather than converting HTML to an attributed string, I need to convert it back to HTML. This can easily be done on Mac as can be seen here: http://www.justria.com/2011/01/18/how-to-convert-nsattributedstring-to-html-markup/ Unfortuately, the method dataFromRange:documentAttributes: is only available on Mac via the NSAttributedString AppKit Additions. My question is how can you do this on iOS? 回答1: Not the 'easy' way, but what about iterating through the attributes of the string using: - (void

UILabel size incorrect for single line of text with lineSpacing and multiple colors

≯℡__Kan透↙ 提交于 2019-12-04 07:04:22
I'm pretty sure this is actually a UIKit bug but want to get some input to see if I'm missing something silly here. Here is the code I have: // single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom! UILabel *brokenLabel = [[UILabel alloc] init]; brokenLabel.backgroundColor = [UIColor greenColor]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"]; [attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:@"text"]];

how to set attributes in NSAttributedString in iOS?

∥☆過路亽.° 提交于 2019-12-04 05:32:00
问题 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