I have a problem regarding to set UIButton frame.
NSString *sample = #"Demo to set display view on label in iPhone";
CGSize maximumLabelSize = CGSizeMake(55,9999);
expectedLabelSize = [sample sizeWithFont:[UIFont systemFontOfSize:20]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"%f",expectedLabelSize.height);
self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, expectedLabelSize.width, expectedLabelSize.height)];
self.label.backgroundColor = [UIColor clearColor];
self.label.numberOfLines = 0;
self.label.text = sample;
[self.label sizeToFit];
So in below uibutton code how to set frame position of text so that only "iPhone" word is clickable?
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(self.label.frame.origin.x,
self.label.frame.origin.y+50,
expectedLabelSize.width,
expectedLabelSize.height);
To make just one word of a UILabel clickable as a hyperlink you would need to know exactly where the UILabel puts that word. That is difficult (not impossible) especially with a multiple line label and especially if you allow resizing of the font to make the text fit.
Why not use a UIWebView and make it a hyperlink instead? Or separate the text that is clickable and put that text into the UIButton by itself, like:
label.text = #"Demo to set display view on label in ";
button.titleLabel.text = #"iPhone";
Note: you should not checkmark this answer if your problem is not solved.
Related
UILabel *label = [[UILabel alloc] init];
label.text = #"Hello";
[label sizeToFit];
CGSize size = label.frame.size;
Usually we get the size of the label in this way, but text in the UILabel does not fill all space in it, there's always some margin around the text. That makes drawing label difficult to be exactly the same as visual draft. Anyone could help ?
for example: If you want to put an icon at the bottom of some text, let the margin between them be, for example 50px, and you write icon.frame = CGRectMake(10,textLabel.frame.size.height + textLabel.frame.origin.y + 50, 100, 100); but since the text can't fill all space in UILabel, so the margin in this way should be a bit larger than it should be. So I want to figure out a better way, thanks.
try with this bellow method which returns dynamic height of UILable with its text content... we this method you can set frame of UILable with its text content...
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like bellow...
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 50)];
lblAddress.text = #"your Text ";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;
lblAddress.font = [UIFont fontWithName:#"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];
See My Blog..
i hope you get some idea from this post...
I have a UILabel which has UILabel attached to it. I add NSString value to it and each time for each cell after the text is over, it appends three dots to the end of the text file.
I am not sure why would I get that. Does any body have faced the similar problems:
Here I define my UILabel:
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
// set the label
label1.backgroundColor = [UIColor clearColor];
label1.font = [UIFont boldSystemFontOfSize:14];
label1.adjustsFontSizeToFitWidth = NO;
self.labelView1 = label1;
[self.contentView addSubview:label1];
[label1 release];
And I add the text as following.
labelView1.text = title;
labelView1.adjustsFontSizeToFitWidth = NO;
If you don't want the label to append '...' when string does not fit label's size set label's lineBreakMode property to UILineBreakModeClip:
label1.lineBreakMode = UILineBreakModeClip;
The problem is that the text is too long to fit the label and you've called
label1.adjustsFontSizeToFitWidth = NO;
which means the text will not be scaled down to fit.
With a width of 15, you can barely fit one letter of font size 14. Make the label bigger or size to fit (or both).
I have a need to display text that includes HTML tags etc and TTStyledTextLabel fits the bill.....but it does not scroll.
I placed one inside a UITextView but this refuses to scroll? If I enter the text directly in the UITextView it scrolls OK but then I see all the HTML unformatted.
Is there a way to set TTStyledTextLabel to scroll?
Thanks
Try putting the TTStyledTextLabel in a UIScrollView.
Alternately, you can consider using a UIWebView directly.
I finally got a suitable work around...
CGSize constraintSize;
CGSize stringSize;
// make an overly big size allowance
constraintSize.width = 300;
constraintSize.height = 2000;
NSString *s = #"this can be text as long or as short as required...;
UIFont *f = [UIFont fontWithName:#"Arial" size:14];
stringSize =[s sizeWithFont:f constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
// create a label to accomodate the text
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(14, 2, stringSize.width, stringSize.height)];
l.text = s;
[l setNumberOfLines:0];
[l sizeToFit];
// now create a TTStyledTextLabel to match the size we just obtained above
TTStyledTextLabel *tl = [[TTStyledTextLabel alloc] initWithFrame:[l frame]];
// set the text making use of links etc
tl.text = [TTStyledText textFromXHTML:l.text lineBreaks:YES URLs:YES];
[tl setBackgroundColor:[UIColor clearColor]];
tl.textColor = [UIColor whiteColor];
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 185, 320, 300)];
// adjust scrollview content size to accomodate the TTStyledTextLabel
[sv setContentSize:CGSizeMake(tl.frame.size.width, tl.frame.size.height)];
[sv addSubview:tl];
[self.view addSubview:sv];
Now I can have an auto sizing TTStyledTextLabel that scrolls ;-)
I am working on a todo list application. It's however i cant seems to align my checkbox image properly with the cell.textLabel.text.
My problem for the code below is the words "test test" got cover by my checkbox image, which only left with "st test"
My code for the checkbox button image like
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5.0, 7.0, 25.0, 25.0)];
UIImage *uncheckedButtonImage = [UIImage imageNamed:#"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];
and my cell textlabel is just
cell.textLabel.text = #"test test";
Is there anyway the align the textLabel text to move a bit to the right?
I have suggested to Add UILabel because in the future if you needed to change the view cell it will be more Dynamic and easier to play around anything new to the Cell.
take a look to the following code snippet:
CGRect DeviceNameFrame = CGRectMake(101, 32, 522, 21);
UILabel *lblDeviceName = [[UILabel alloc] initWithFrame:DeviceNameFrame];
lblDeviceName.backgroundColor = [UIColor clearColor];
lblDeviceName.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
lblDeviceName.text = #"test test";
lblDeviceName.lineBreakMode = UILineBreakModeMiddleTruncation;
[cell.contentView addSubview:lblDeviceName];
[lblDeviceName release];
I hope this will help you.
The simplest solution would be to look at the "Indentation" properties of UITableViewCell. See the documentation for more info. Subclassing the cell and adding your own UILabel would give you the most control though.
I am building an app in which I need to resize the button as per the title length. I wrote the following code
`UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
newButton.backgroundColor = [ UIColor clearColor];
newButton.titleLabel.backgroundColor = [ UIColor whiteColor];
[newButton setTitle:#"devsri" forState:UIControlStateNormal];
newButton.titleLabel.textColor = [ UIColor blackColor];
CGSize expectedLabelSize = [newButton.titleLabel.text sizeWithFont:newButton.titleLabel.font];
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height);
The above code do resize the table but the button no more remains round rectangle in view. Kindly let me know what is wrong in the above code.
Thanks in advance!!
I got the bug in here. I was initializing the frame with the size of the titlelabel that was eventually bigger than the original button in width. Thus the line
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height);
as
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width + 15, expectedLabelSize.height);
This will widen the button enough to accomodate the new size of the label.
:)