My app is using custom font to display an information for the whole app.
But there is some problems with their base line like in the image below:
I need to fix "X 4" to be centered in vertical red box.
How can I fix this?
Set attributed string with adjusted baseline offset like so in Swift:
let sttrStr = NSAttributedString.init(string: "your text",
attributes:[NSAttributedStringKey.baselineOffset: -10])
textLabel.attributedText = attrStr
or like so in ObjC:
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:#"your string"
attributes:#{ NSBaselineOffsetAttributeName : #-10 }];
textLabel.attributedText = attrStr;
Per SO answer here
You can create a UILabel and set its size same as the red box. And then set it's text alignment as center:
youLabel.textAlignment = NSTextAlignment.Center
Another not so good option is to make a UIButton of same size as red box and set its UserInteractionEnabled to No. This way your text will be positioned in the center horizontally and vertically. You can also set it's setContentVerticalAlignment property
Related
I got a UItextView, I want the first line of its content to have larger size than the rest.
Like the following image.
Example
But I don't see any font size related attribute in paragraph attributes.
So, how to do that?
Thanks for any advices.
Use NSMutableAttributedString..
NSMutableAttributedString *firstLine = [[NSMutableAttributedString alloc]initWithString:#"First Line\n" attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:20]}];
NSAttributedString *secondLine = [[NSAttributedString alloc]initWithString:#"Second Line" attributes:#{NSFontAttributeName : [UIFont systemFontOfSize:16]}];
[firstLine appendAttributedString:secondLine];
yourTextView.attributedText = firstLine;
Have you tried attributed String? You just need to know how long your first line will be.
// 16.0 is your standard font size for the textView
let text = NSMutableAttributedString(string: "your whole text for the textview", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16.0)])
// 23.0 is the size for your first line
text.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 23.0), range: NSRange(location: 0, length: firstLineLength))
textView.attributedText = text
Select your label then tap on the attributes inspector .
Select the Attributed
You will see the changes just like this -
Select the first line just like -
Then tap on the text icon
-
Now change the font and press enter after that, changes will trigger in your stroyboard label. Do same for others.
How to add background color and rounded corners for Strings in iOS?
like this picture below:
Can't do it with NSAttributedstring. So, should I use CoreText? I found this answer but still don't know how to do it.
String is just a data in text format, it's not visual. You need labels, those are labels there. And then set your string to that label.
let label = UILabel()
label.backgroundColor = .green
label.text = "Label" // You set your string to your label
label.layer.cornerRadius = 5
label.layer.borderColor = .clear
label.layer.borderWidth = 1
label.layer.clipsToBounds = true
Code above creates a label and sets everything you need to achieve it like in the picture. Now you need to add this label into your view or you can set these properties to an existing label you got.
EDIT:
If you want to make individual text inside a text view colorful, you can use TTTAttributedLabel and set an attributed text with background to individual pieces of text.
You can find TTTAttributedLabel here.
CornerRadius: kTTTBackgroundCornerRadiusAttributeName
BackgroundColor: kTTTBackgroundFillColorAttributeName
Example usage:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:TTTAttributedTestString()];
[string addAttribute:kTTTBackgroundFillColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [string length])];
I have created a label programmatically.it's with is equal to the device width and I have aligned it to right.so it shows the text from right.
like this
titleLabel.textAlignment = NSTextAlignmentRight;
then I gave a text to the label.
titleLabel.text = #"Flight Summary";
but I want to keep a space after y letter in summary, without decreasing the width of the label.I tried with using string format like this.
titleLabel.text =[NSString stringWithFormat:#"%# ", #"Flight Summary "];
but nothing happned.how can I do that.hope your help for this.thanx.
To reduce the complexity of subclassing, you can take UIView and UIlabel, set frame of UIView to screen width and take UILabel frame as screenwidth - 8 (or whatever pixels is appropriate for you). Manage the frames and add both to mainview, this way you will be able to achieve the look.
Try this
titleLabel.text = #"abcd exadgdf \u{200c}""
You can also do one more thing. You can use attributed string and set attributed text to titlelabel without formatting string like this
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"ABCD "];
titleLabel.attributedText = attrString;
I have a UILabel that I need to convert to a UITextView because reasons. When I do this, the text is not positioned the same, despite using the same (custom) font.
I found that if I set:
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
This gets the text very close, but if I superimpose the UITextView over top of the UILabel, I see the text positioning get farther apart with each new line.
The UILabel is green, the UITextView is black. This is using NSParagraphStyle to set min and max line height to 15.
I've played with setting the paragraph style and min/max line height, but I haven't been able to match it exactly. I'm not a printer, so I don't necessarily understand all of the font related terms in the documentation for NSLayoutManager and NSTextContainer and all that.
I only need to support iOS 7 and up.
I'm not going to switch to some crazy CoreText-based custom widget or use some random third party library. I'm okay with close enough if I have to. But it seems like there should be some combination of random properties to make them layout the same.
I took the solution for line spacing found at this link and applied it to your issue. I managed to get it incredibly close by adjusting the lineSpacing property. I tested with HelveticaNeue size 13 and managed to get it to line up as shown in the screen shot below.
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -0.38;
NSDictionary *attrsDictionary =
#{ NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:13.0f],
NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attrsDictionary];
I've been able to successfully 'impersonate' a non-editable multiline UILabel (as it happens, in a UITableViewCell subclass) with an equivalent editable multiline UITextView using the following :
_textView = UITextView.new;
_textView.font = _label.font;
_textView.textColor = _label.textColor;
_textView.textAlignment = _label.textAlignment;
_textView.backgroundColor = UIColor.clearColor;
_textView.textContainer.lineFragmentPadding = 0;
_textView.textContainerInset = UIEdgeInsetsZero;
and to make it behave well when doing actual edits, add the following to your UITextViewDelegate:
- (void)textViewDidChange:(UITextView *)textView
{
...
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
[textView scrollRectToVisible:[textView caretRectForPosition:textView.endOfDocument] animated:NO];
}
I have a UITextView which displays a fair amount of text. I need to horizontally center some, but not all, of the lines of text in the text view.
I've tried setting the NSParagraphStyleAttributeName with an NSParagraphStyle instance whose alignment property is NSTextAlignmentCenter on the range of the line to be centered. I'm also setting the font so the centered line is bold. When I do this using the code below, the relevant line is bolded, but remains left aligned.
if (shouldCenterLine) {
NSMutableParagraphStyle *centerStyle = [[NSMutableParagraphStyle alloc] init];
centerStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{NSParagraphStyleAttributeName : centerStyle,
NSFontAttributeName : boldFont};
[attributedString setAttributes:attributes range:lineRange];
}
If I apply this same paragraph style attribute to the entire contents of the text view, the text ends up centered as expected.
Is it possible to convince UITextView to center specific subranges of its text? Is the only solution to move to a UIWebView rendering generated HTML? Note that this is for an app still supporting iOS 6, so unfortunately I'm not able to use Text Kit.
Try including the line returns around the the line that you'd like centered within the range that you apply the style to.