I've seen a few other questions discussing wrapping text/multi-line content in segmented controls but all of the solutions are either super janky or deprecated since 9.0 (so please don't mark this as a duplicate unless you can cite a 9.0+ post with a solution).
All I want to do is wrap text within a segment to the next line... is there a clean/correct way to do this in Objective-C? Thanks in advance!
I haven't tested, but this seems like the rational approach...
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributtes = #{ NSParagraphStyleAttributeName: paragraphStyle };
[mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
Related
I want to align justify my text in UILabel but it seem like not work. However, another align is left,right,center work.
I'm using XCode 7.2. I have tested on simulator and real device but it produce same problem
Align Justify
My text:
Don't worry, your data will not be sold.Don't worry,your data wills not be sold. Connecting your accounts will benefit your E score and your profile viewing experience. Don't worry, your data will not be sold.Don't worry, your data wills not be sold. Connecting your accounts will benefit your ECT score and your profile viewing experience.
with font : Helvetica Neue 13.0 and trailing/leading: 10
Same problem if I use align in here to justify text
I don't know why this happened to me. Please give me some instruction for fix it. Any help would be great appreciated
It seems like a bug of UILabel,but you can fix it with a tiny change in your storyboard.
Click the more button in the same line of NSTextAlignments,add a little Head Indent ,such as 0.1 .
Your UILabel will work just fine.
This should work. Here is what I get from the simulator:
What I've done:
Drag & drop an UILabel on the storyboard and add some constrains, and colours as well.
Set the text as "attributed"
Put your text in the text field.
Click on justify.
Change the police
Numbers of lines 0.
At this point you should have this:
Now from the storyboard to your controller add an IBOutlet (Ctrl + drag it to the top of your controller). It should be like this:
Now add some code in your viewDidLoad fct:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified
let attributedString = NSAttributedString(string: label.text!, attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
label.attributedText = attributedString
label.numberOfLines = 0
The last thing to do is to run your simulator to see if it does what you are expected:
P.S: With xCode 7.2 works definitely. It works for me on both version.
Here is one solution to solve my problem right now. I set the alignment justified for my UILabel programmatically and it work
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;
NSDictionary *attribute = #{
NSParagraphStyleAttributeName: paragraph,
NSFontAttributeName: self.describesLabel.font,
NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:0]
};
NSAttributedString *attributeMessage = [[NSAttributedString alloc] initWithString:self.describesLabel.text attributes:attribute];
self.describesLabel.attributedText = attributeMessage;
Don't use attributed text just use Plain text and select all the text and make it justify. I am facing the same problem fixed it by changing attributed to plain text in Storyboard.
If it doesn't work then you have to fix it by code :-
Using Attributed Text:-
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentJustified;
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:yourFont,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName, nil];
NSAttributedString *attributeMessage = [[NSAttributedString alloc] initWithString:yourTextString attributes:attrsDictionary];
self.yourLabel.attributedText = attributeMessage;
Using Plain Text :-
self.yourLabel.textAlignment = NSTextAlignmentJustified;
self.yourLabel.font = yourFont;
self.yourLabel.text = yourTextString;
Hope It helps...
You can do it programatically if its not working from storyboard or nib
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentJustified;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:#"Your text here"];
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
paragraphStyle, NSParagraphStyleAttributeName ,
[NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName,
nil]];
self.yourLabel.attributedText = string;
I am facing one issue that is - I have one label like "1. The Seibl is a software made by the some company and can be purchased at $500"
When it comes to iPhone 4s, the label is printing second line and second line is starting exactly under "1.". I would like to give space/margin/space so that label looks like numbering format.
Try this solution. It might helps you.
Use an NSAttributedString for your label, and set the headIndent of its paragraph style:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.headIndent = 14;
NSDictionary *attributes = #{
NSParagraphStyleAttributeName: style
};
NSAttributedString *richText = [[NSAttributedString alloc] initWithString:#"So this UILabel walks into a bar…" attributes:attributes];
self.narrowLabel.attributedText = richText;
self.wideLabel.attributedText = richText;
Result:
Take a look at using a TextView instead, and modifying its textStorage property to define an exclusion area so that a new line is inset. Here's a link to the documentation.
So I have a UILabel that may or may not go to a second line, depending if it is on iPhone or iPad. What I would like to accomplish is to have it indent on the second line to line up correctly, if needed.
On iPad it will almost never need the second line break, and depending on which iPhone it is running on, it may or may not. So, in essence, I need a way to dynamically indent the second line, only when there is a second line.
Use an NSAttributedString for your label, and set the headIndent of its paragraph style:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.headIndent = 14;
NSDictionary *attributes = #{
NSParagraphStyleAttributeName: style
};
NSAttributedString *richText = [[NSAttributedString alloc] initWithString:#"So this UILabel walks into a bar…" attributes:attributes];
self.narrowLabel.attributedText = richText;
self.wideLabel.attributedText = richText;
Result:
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];
}
How to increase spacing between lines in UITextView?
I wish to use default system font,i.e., Helvetica with size 15.
Declare you implement the protocol by adding
<NSLayoutManagerDelegate>
to your interface. Then, set:
yourTextView.layoutManager.delegate = self;
Then override this delegate method:
- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 5; // Line spacing of 19 is roughly equivalent to 5 here.
}
UPDATE:
I recently discovered that this can also be done using the NSMutableParagraphStyle setLineSpacing: API.
In an effort to always provide useful copy-paste code-snippet awesomeness, here you go!
NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc] init];
[myStyle setLineSpacing:myLineSpacingInt];
[myString addAttribute:myDesiredAttribute value:myStyle range:myDesiredRange];
[myViewElement setAttributedText:myString];
^myViewElement can be a UITextField, UILabel, or UITextView.
In IOS6+ you can set the typing attributes for a UITextView
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[textView setTypingAttributes:attrsDictionary];