Any way to bold part of a NSString?

Any way to bold part of a NSString?

Related searches

Is there any way to bold only part of a string? For example:

Approximate Distance: 120m away

Thanks!


What you could do is use an NSAttributedString.

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName 
                   value:boldFontName
                   range:boldedRange];

[attrString endEditing];
//draw attrString here...

Take a look at this handy dandy guide to drawing NSAttributedString objects with Core Text.

NSAttributedString by example – Hacking with Swift, Format text in a variety of ways with this one useful class. For example, if you have some text and the user wants to make part of it bold  If you want to bold some uncontinuous specific characters in a text string, you can try to apply Superscript/Subscript function of Kutools for Excel. 1. Select the cell you want to bold the specific characters, and click Kutools > Format > Superscript/Subscript.


As Jacob mentioned, you probably want to use an NSAttributedString or an NSMutableAttributedString. The following is one example of how you might do this.

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Approximate Distance: 120m away"];
NSRange selectedRange = NSMakeRange(22, 4); // 4 characters, starting at index 22

[string beginEditing];

[string addAttribute:NSFontAttributeName
           value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0]
           range:selectedRange];

[string endEditing];

Formatting strings with NSAttributedString, Your browser does not currently recognize any of the video formats available. able to add formatting like bold or italics, select from different fonts, or add some color, and for Attributed strings are made up of two parts: a plain Swift string, plus a of attributes that describe how various segments of the string are formatted. To bold a string without hardcoding its font, you can use the StrokeWidth attribute with a negative value: An NSString is just a data container. It doesn't contain any details about presentation concerns. It sounds like what you probably want to do is bold part of the UILabel that is being used to display your string.


If you do not want to bother with fonts (as not every variation of font contains "Bold"), here is another way to do this. Please be aware, this is currently only available on OS X...:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:"Approximate Distance: 120m away"];
[attrString beginEditing];
[attrString applyFontTraits:NSBoldFontMask
                      range:NSMakeRange(22, 4)];
[attrString endEditing];

How to easily present strings with bold text and links in Cocoa , Just about any UI component that presents strings these days can take an __​block NSString *s = [self copy];. // Part 1. Process URL-s. The most native way to do this in Cocoa is with NSAttributedString. You can set the attributed string source, and then add URL and different font ranges to the attributed string, and it all works just fine.


The code above gave me crash when I created UILabel with this attributedString.

I used this code and it worked:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange boldedRange = NSMakeRange(0, 1);
UIFont *fontText = [UIFont systemFontOfSize:12]; //[UIFont fontWithName:@"Lato-Bold" size:12];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
[attrString setAttributes:dictBoldText range:boldedRange];

Any way to bold part of a NSString?, What you could do is use an NSAttributedString . NSString *boldFontName = [[​UIFont boldSystemFontOfSize:12] fontName]; NSString  I would like to Bold text that is inside a formula while running Excel 2007. Here is the formula. ="Upon receipt by the undersigned for a check from XYZ in the sum of "&DOLLAR('G702'!E28,2)&" payable to "&'G702'!C13&" and when the check has been properly endorsed and has been paid by the bank upon which it is drawn, this document shall become effective to release any mechanic’s lien, stop


Swift

Also includes getting the range of the string you want to embolden dynamically

let nameString = "Magoo"
let string = "Hello my name is \(nameString)"

let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(14.0),NSForegroundColorAttributeName: UIColor.black]
let boldAttribute = [NSFontAttributeName:UIFont.boldSystemFontOfSize(14.0)]

let attributedString = NSMutableAttributedString(string: string, attributes: attributes)

let nsString = NSString(string: string)
let range = nsString.rangeOfString(nameString)

if range.length > 0 { attributedString.setAttributes(boldAttribute, range: range) }

someLabel.attributedText = attributedString

I want to bold certain areas of a text (only part of the text). For example lets say I have the phrase ”Mr John Smith is a software engineer and is 42 years old”, where the name and age are taken from cells in the sheet 1 “ these I want to appear as bold- and I also need the ”software engineer” to appear


So far, I have found something called NSScanner, though I haven't really found a way to implement this yet. I'm pretty sure there's another way to do this, though, so if anyone could give any insight, it would really help! (By the way, as the title implies, the HTML from above is in a NSString variable type)


Re: Is there any way to bold part of an expression label ?? It's not possible - within the label you could only have one font-type, font-size and so on. As alternatively to get a better differentiation you could use upper-chars like: "EMPLOYEE count".


The result may be achieved in two different ways. Method 1: Count digits and extract that many chars. The easiest way to split text string where number comes after text is this: To extract numbers, you search the string for every possible number from 0 to 9, get the numbers total, and return that many characters from the end of the string.