TailTruncation is not working with OHAttributedLable - iphone

I am having OHAttributedLable like following
lblHeaderTitle =[[OHAttributedLabel alloc]init];
[lblHeaderTitle setBackgroundColor:[UIColor clearColor]];
[lblHeaderTitle setFrame:CGRectMake(15, 8,(WidthForBoldText(txt1))+45, height)];
//[lblHeaderTitle setNumberOfLines:2];
[lblHeaderTitle setLineBreakMode:UILineBreakModeTailTruncation];
[lblHeaderTitle setNumberOfLines:0];
lblHeaderTitle.attributedText = attrStr;
it is displaying all in one line and remaining part is not visible
now the problem is that i am not able to wrap or truncate text in this lable can anybody help me with this.
what i want is if my text is
"Abcdefghijklmnopqrstuvwxyz" then it will display
"Abcdefghijklmn..." or
"Abcdefghijklmn
opqrstuvwxyz"
anything will work.
Thanks in advance.

lableName.numberOfLines = 0;
lableName.lineBreakMode = UILineBreakModeWordWrap;

Related

How to highlight only text in UILabel - IOS

I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.

UITextField placeholder shifts when subclassed

I am using a subclassed UITextField to use a custom font. When I set a placeholder for that textfield it gets shifted up a little, but the when I start entering the text, the frame of text has no problem. Is there a way to fix the placeholder shifting issue.
self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = #"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;
I had same issue and I tried setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter which solved my issue.
UITextField *txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(15,55, 255, 25)];
[txtPassword setPlaceholder:#"Password..."];
[txtPassword setBackgroundColor:[UIColor whiteColor]];
[txtPassword setFont:[UIFont fontWithName:#"Helvetica" size:15]];
[txtPassword setSecureTextEntry:YES];
[txtPassword setTextAlignment:NSTextAlignmentCenter];
[txtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[txtPassword becomeFirstResponder];
Hope this helps.

iPhone/Objective - C - How to display the whole text of a long string in a UILabel

I have a very long string which is something like that below...
[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3],[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4]],[sqrt(sqrt(221)+15),sqrt(-sqrt(221)+15)]
How can i display the whole thing in a UILabel with more than one lines (0)?
Also, although there are no linebreaks ("\n") in the string, the result is displayed like below....
[[sqrt(221)-10,-sqrt(221)- 10],[11,11]],[[rootof([[474368400,2 37184200,- 125944810200,258530778000],[1, 0,- 472,884,42215]])/(sqrt(221)*78270
Any help?
PS I could upload an image but i am not allowed beacause i am a new user...
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
set the UILabel as a multiline
MyLbl.numberOfLines = xxx;
if you have not enough space for set the UIlabel then also set the textView As an not editable property:
Mytxt = [[UITextView alloc]initWithFrame:CGRectMake(xx.0, x.0, xx.0,xx.0)];
Mytxt.transform = transform;
Mytxt.adjustsFontSizeToFitWidth = YES;
Mytxt.textColor = [UIColor blackColor];
Mytxt.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[Mytxt setFont:[UIFont fontWithName:#"Arial" size:xx.0]];
Mytxt.backgroundColor = [UIColor clearColor];
Mytxt.delegate = self;
Mytxt.userInteractionEnabled=YES;
Mytxt.editable=NO;
i hope you get the answer.
You can use UITextView with auto size with non editable mode instead of UILabel (or)
CGSize labelSize = yourString sizeWithFont:cellFont
constrainedToSize:CGSizeMake(190.0f, MAXFLOAT); lineBreakMode:UILineBreakModeWordWrap];
This labelsize will dynamically define the label height you can assign to your label
try this code
NSString *yourString = #"[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])";
UILabel *lblNameDesc=[[UILabel alloc] initWithFrame:CGRectMake(55, 10, 250, 21)];
[lblNameDesc setNumberOfLines:0];
lblNameDesc.text=yourString;
[lblNameDesc setBackgroundColor:[UIColor clearColor]];
[lblNameDesc setTextColor:[UIColor colorWithRed:64.0/255.0 green:64.0/255.0 blue:64.0/255.0 alpha:1.0f]];
[self.view addSubview:lblNameDesc];
// [lblNameDesc release];
[lblNameDesc sizeToFit];
supoose your string is. make your fond and width what ever you want:-
NSString longString = #"dsjkghsdkjnfgvsdjgsl,v.........................";
CGSize maximumLabelSize = CGSizeMake(200,MAXFLOAT);
CGSize expectedLabelSize = [longString sizeWithFont:[UIFont fontWithName:#"Aial-BoldMT" size:15.0] constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, expectedLabelSize.height)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = longString ;
may this will help you
Try This...
textLabel.lineBreakMode = YES;
textLabel.numberOfLines = 10; // or many that you need...
You want UILineBreakModeCharacterWrap if you want your text to be evenly distributed along several lines (so it doesn't try to break at work boundaries).

Setting truncation mode for UINavigation title text

I want to configure the truncation mode for the text that appears in UINavigationItem.
I've come across a couple of different solutions of configuring the font size but not sure how to make use of the UILineBreakModeHeadTruncation that exists on NSString.
How I've been able to configure the font size:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
(Based on the recommendation from here)
If I call this in loadView or viewDidLoad, it seems to work but outside of that method, after the view has been loaded, it doesn't seem to work. Not sure why though.
What have you tried, and what's not working? You should be able to do:
label.lineBreakMode = UILineBreakModeHeadTruncation;
Or to wrap to multiple lines:
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;

How can I make text in a UILabel non-centered?

I have a UILabel:
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
[descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[descriptionLabel setTextColor:[UIColor whiteColor]];
descriptionLabel.numberOfLines = 0;
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
[self addSubview:descriptionLabel];
If the text is only 1 line long it will appear in the middle of the label.
Is there anyway I can have the text display from the top left corner of the label instead of the middle center?
You can set
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
or if you prefer dot syntax
descriptionLabel.textAlignment = UITextAlignmentLeft;
for left/right alignment set the textAlignment property of UILabel to either UITextAlignmentLeft or UITextAlignmentRight.
for more fine tuning you can override drawTextInRect: