I have a UIView that I'd like to add several bits of text to. I have used a UITextView but I think that's overkill as it doesn't need to be editable. I thought about using a UILabel or a UITextField, but I don't see how you tell the superview where to position the UILabel or UITextField within itself. I want the lowest footprint object that will let me put text of a font/color/size of my choosing in my UIView where I want it. Not too much to ask, eh?
The simplest approach for you would be:
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[yourSuperView addSubview:yourLabel];
Building or populating Views in your code will probably require you to use CGRectMake a lot.
As its name says, it creates a rectangle that you can use to set the relative position (relative to the borders of your superview) and size of your UIView-Subclass (in this case a UILabel).
It works like this:
yourLabel.Frame = CGRectMake(x, y, width, height); //x,y,width,height are float values.
x defines the spacing between the left hand border of the superview and the beginning of the subview your about to add, same applies to y but relating to the spacing between top-border of your superview.
then width and height are self-explanatory i think.
Hope this gets you on the track.
Instead of finding a way to tell the view where to position the UILabel, you can tell the UILabel where to position itself in the view by using "center".
E.g.
myLabel.center = CGPointMake(0.0, 0.0);
Hope you'll be able to use UILabel, for me it's the basic form of a flexible non editable text.
For Swift:
let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
yourLabel.textColor = UIColor.whiteColor()
yourLabel.backgroundColor = UIColor.blackColor()
yourLabel.text = "mylabel text"
yoursuperview.addSubview(yourLabel)
This question is old, but for a pure UIView text option without using UILabel or UITextField (as all the other answers describe, but the question is how to do it without them), drawRect in a subclassed UIView works for me. Like so:
- (void)drawRect:(CGRect)rect{
NSString *string = #"Hello World!";
[string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
}
This routine displays a text at a X-Y position
-(void)placeText:(NSString *)theText:(int)theX:(int)theY {
UILabel *textLabel;
// Set font and calculate used space
UIFont *textFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize textStringSize = [theText sizeWithFont:textFont constrainedToSize:CGSizeMake(300,50) lineBreakMode:NSLineBreakByTruncatingTail];
// Position of the text
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(theX+OFFSETIMAGEX-(textStringSize.width/2), theY+OFFSETIMAGEY-(textStringSize.height/2), textStringSize.width,textStringSize.height)];
// Set text attributes
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor orangeColor];
textLabel.font = textFont;
textLabel.text = theText;
// Display text
[self.view addSubview:textLabel];
}
It might be late but here is what I use:-
CGRect labelFrame = CGRectMake(120,300, 530, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
//If you need to change the color
[myLabel setTextColor:[UIColor whiteColor]];
//If you need to change the system font
[myLabel setFont:[UIFont fontWithName:NULL size:23]];
//If you need alignment
[myLabel setTextAlignment:NSTextAlignmentCenter];
// The label will use an unlimited number of lines
[myLabel setNumberOfLines:0];
//Add label view to current view
[self.view addSubview:myLabel];
NSString *someString = #"Sample String, Yarp!";
myLabel.text = someString;
add a UILabel to your View. then override the View's layoutSubviews method.
Related
I'm seeing lots of deprecated answers for this question:
How do I calculate the number of lines in use in a UILabel based of its set text?
I know that I need to set the UILabel to have bounds that resize with word wrapping. In this way, I could detect the height of my UILabel and adjust an NSLayoutConstraint for the height of my UITableViewCell. Basically my problem plain and simple is:
How can I determine my UILabel's number of lines in use(based of descriptionLabel.text) or height in order to resize My UITableView's UITableViewCells which contain my UILabel.
Currently I have used this code:
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 270, 65)];
descriptionLabel.textColor = [UIColor blackColor];
descriptionLabel.numberOfLines = 0;
descriptionLabel.adjustsFontSizeToFitWidth = YES;
Try using the following code
NSAttributedString *attrStr = ... // your attributed string
CGFloat width = 300; // whatever your desired width is
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
//rect is the height of UILABEL which u can used as height of label or table view row or etc
I solved my problem very simply with this code:
CGSize maxSize = CGSizeMake(290.0f, CGFLOAT_MAX);
CGSize requiredSize = [descriptionLabel sizeThatFits:maxSize];
[descriptionLabel setFrame:CGRectMake(15, 50, requiredSize.width, requiredSize.height)];
NSLog(#"THE HEIGHT OF THIS DESCRIPTIONLABEL IS: %f",cell.frame.size.height);
I'm a beginner in iOS development, and I have trouble resizing automatically a UITextView.
I created a Master-Detail Application. On my DetailViewController.xib I added a ScrollView that encompass a Label for the title, an image and under this image, a TextView in order to put a description that the user posted.
The problem is that the description depends on what my WebService returns, so I need it to be resized automatically.
Here's my code:
- (void)configureView
{
...
self.detailDescriptionTextView.text = [self.detailItem valueForKey:#"adDescription"];
CGRect frame = self.detailDescriptionTextView.frame;
frame.size.height = self.detailDescriptionTextView.contentSize.height;
self.detailDescriptionTextView.frame = frame;
)
The problem is that the TextView gets the content correctly but it doesn't resize at all.. I looked for an answer for a while and most of answers are what I tried...
Thanks for your help.
EDIT : I realized something. In order to put some elements under the description, I've put a very small TextView for the description in the Interface Builder, is it a problem? If it is, how could I put other elements under this TextView, because there would be no more space on the Interface Builder no?
EDIT 2 : I finally succeeded. It seems like the problem was that I created the TextView by Interface Builder. By creating it programmatically, it worked perfectly. Here's my code if it can help someone:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = [self.detailItem valueForKey:#"adDescription"];
textView.font = [UIFont fontWithName:#"Hevlatica" size:14];
[self.scrollView addSubview:textView];
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;
Thanks for your help.
First calculate the content height using
NSString *content = #"Hello how are you.";
CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(yourWidth, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
and then set the frame of textView
[self.textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];
Or
Add the content to your textview and then try this
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Hope this Helps !!!
CGSize constraint = CGSizeMake(690.0, 2000.0);
int h=10;
CGSize size_txt_overview1 = [[self.detailItem valueForKey:#"adDescription"] sizeWithFont:[UIFont fontWithName:#"Helvetica" size:18] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
frame.size.height = size_txt_overview1.height;
self.detailDescriptionTextView.frame = frame;
Is it possible to draw a line with different width through some cells in UITableView?
Something like this:
I'm using Xcode 4.6 and target is iOS5+
Rough idea on how it might work. You'll need to manage the red line when the reusable cell content changes.
CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font];
CGFloat y = cell.contentView.frame.size.height / 2;
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5,y,size.width, 3)];
line.backgroundColor = [UIColor redColor];
[cell.textLabel addSubview:line];
make some lines(like image, etc) above the tableViewCell
like [cell.contentView addSubView:$LINE_IMAGE_VIEW]
you can get the label's width like this
CGFloat width = cell.titleLabel.frame.size.width
then assign that width to custom line image.
This will strike through the whole cell. In case you need it to look like completed task:
CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];
You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.
I want to have a UIButton with two lines of text, with each line in different color. Is it possible to have like that ?
There are 2 Approaches I can think of :
Approach 1 (easy) : Make it as an image button
Approach 2 (hard) : Make a custom UIButton, with 2 separate UILabel, such that you can configure different colors for them
To achieve Approach 2, you first create a class with UIButton as superclass. Then, override - (void)drawRect method. In order not to repeat answer in SO, please read this: How to override -drawrect in UIButton subclass?
Hi this answer only applicable in above ios 6...
Use ios 6 's attributed text to achieve this...
NSString* infoString=#"This is an example of Attributed String";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:#"Helvetica-Bold" size:20.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength/2)];
[self.attribButton setAttributedTitle:attString forState:UIControlStateNormal];
self.attribButton.titleLabel.numberOfLines=2;
Try like this...
In Swift
var main_string = "Hello World"
var string_to_color = "World"
var range = (main_string as NSString).rangeOfString(string_to_color)
var attributedString = NSMutableAttributedString(string:main_string)
attributedString.addAttribute(NSForegroundColorAttributeName, value: appSingleton.appRedColor , range: range)
self.celciusButton.setAttributedTitle(attributedString, forState: UIControlState.Normal)
yes sure..!!! make an image what you exactly want to show then set the image as an background of that button..! its simple...
First, make your button of "custom" type. If you need to do that programmatically:
UIButton* myButton = [UIButton buttonWithType: UIButtonTypeCustom];
myButton.frame = CGRectMake (x,y,width,height);
Now make your labels:
UILabel* line1 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,buttonWidth,buttonHeight/2)];
UILabel* line2 = [[UILabel alloc] initWithFrame:CGRectMake(0,buttonHeight/2,buttonWidth,buttonHeight/2)];
And assign the text colors you want:
line1.textColor = [UIColor blueColor];
line2.textColor = [UIColor redColor];
Then add the labels to the button, and the button to your view:
[myButton addSubview:line1];
[myButton addSubview:line2];
[self.view addSubview:myButton]; // only if you've created the button programmatically
I have created a UILabel that displays a single large character. Even with clipsToBounds = NO; I still get clipping.
See link: http://img341.imageshack.us/img341/5310/screenshot20100814at243.png
I used the following code:
CGSize fBounds = [myLabel.text sizeWithFont:cFont];
To get what should be the bounding rectangle of the font. And the label is drawn with:
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
myLabel.clipsToBounds = NO;
myLabel.numberOfLines = 1;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontSize = 10;
myLabel.text = #"A";
myLabel.font = [UIFont fontWithName:#"CourierNewPSMT" size:300];
myLabel.textColor = [UIColor blackColor];
myLabel.backgroundColor = [UIColor colorWithRed:1 green:.5 blue:0 alpha:.5];
In the image below, the size returned from sizeWithFont is rendered by the semi-transparent blue rectangle overlay. As you can see, with an italic font (in this case Verdana-BoldItalic), the character extends past what sizeWithFont returns. Further, the UILabel's frame (the orange color) also clips the character. Thoughts? Maybe I could override some text drawing routine. Also, not sure if this is the same problem as here:
UIButton.titleLabel clipping text problem
Use attributed text + indent...
Looks like this is an apple problem. Ended up doing custom drawing with CoreText.