nsattributedstring

setting NSAttributed String attribute overrides substring attributes

偶尔善良 提交于 2020-01-23 10:36:30
问题 I have created a mutable string which will look like @" testMeIn:greenColor:Different:greencolor:Colors " NSMutableAttributedString *mutableText = [[NSMutableAttributedString alloc] initWithAttributedString:myString]; UIColor *foregroundColor = [UIColor blackColor]; NSString *key = NSForegroundColorAttributeName; [mutableText addAttribute:key value:foregroundColor range:NSMakeRange(0, myString.length)]; When I add Attribute foregroundColor , the existing green color in substring gets

Add a click event to some text in ios NSString

谁说胖子不能爱 提交于 2020-01-22 23:06:53
问题 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?

Toggle selectedRange attributes in UITextView

£可爱£侵袭症+ 提交于 2020-01-22 02:23:25
问题 I have created a button that I want to check if text is selected then if so toggle bold and unbold over the selectedRange when tapped. At the moment my code will just change the selectedRange to bold and I can't undo it or check if there is a selection. How can I achieve this? func bold() { if let textRange = selectedRange { let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.bold)] noteContents.textStorage.addAttributes(attributes as

NSAttributedString: Fit image to container

时光总嘲笑我的痴心妄想 提交于 2020-01-21 03:19:52
问题 I have a NSAttributedString which is made from HTML and it displays some images. The problem is that the images are bigger than the container and I wonder how to fit them in it. Thanks for your help 回答1: I finally found how to do that: content.enumerateAttribute(NSAttachmentAttributeName, inRange: NSMakeRange(0, content.length), options: NSAttributedStringEnumerationOptions(0)) { (value, range, stop) -> Void in if let attachement = value as? NSTextAttachment { let image = attachement

Aligning text vertically center in UITextView

為{幸葍}努か 提交于 2020-01-17 04:47:07
问题 I am setting some attributed text to textview, and giving line. I am trying to set baseline alignment vertically center but unable to set that. How can I set the text vertically center in textview. 回答1: First add/remove an observer for the contentSize key value of the UITextView when the view is appeared/disappeared: override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) textView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New,

Display edited NSString in order

六月ゝ 毕业季﹏ 提交于 2020-01-15 12:13:51
问题 I have been working on this for a few days with help from this great community. I have a NSArray that I need to edit NSStrings within. I have managed to detect a marker in the string and make it bold. However now I am trying to display the strings in the order that they are within the NSArray whilst maintaining the Bold that was added to the specific strings. I can display the individual Bold String ' string ' but I need it to be in order that it is within the array. I know of

Display edited NSString in order

霸气de小男生 提交于 2020-01-15 12:13:43
问题 I have been working on this for a few days with help from this great community. I have a NSArray that I need to edit NSStrings within. I have managed to detect a marker in the string and make it bold. However now I am trying to display the strings in the order that they are within the NSArray whilst maintaining the Bold that was added to the specific strings. I can display the individual Bold String ' string ' but I need it to be in order that it is within the array. I know of

convert NSAttributedstring (with NSTextAttachment having image) to html in ios obj c

我的未来我决定 提交于 2020-01-14 05:44:27
问题 I know how to convert the common NSAttributedString (which has no image) into HTML. I set the Images as NSTextAttachment to NSAttributedString . How could I convert the whole attributed string into HTML? UIImage *image = [UIImage imageNamed:@"Test.png"]; NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = image; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:htmlString]; NSAttributedString *attstringImage

Prevent line break in a NSAttributedString

帅比萌擦擦* 提交于 2020-01-13 05:26:27
问题 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)

NSAttributedString Writing Direction

ⅰ亾dé卋堺 提交于 2020-01-11 08:45:32
问题 I am trying to change the write direction in an NSAttributedString. However, I have a really hard time figuring out how to do it. CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"ArialRoundedMTBold", 16, NULL); NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,(NSString *)kCTFontAttributeName, nil]; CFRelease(fontRef); NSAttributedString *attString=[[NSAttributedString alloc] initWithString:self.stringMap attributes:attrDictionary]; self