I have a UILabel and if i adjust the size of the text i can make it look loke a UITextView however the left margin is different, on the UIlabel the text is right up against the left border where the UITextView has a slight margin. How do i adjust the UILabel so that when these controls are placed above one another, they look consistent?
Simply change the label's frame:
CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;
Of course replace MARGIN with whatever you want your margin to be.
Or you could subclass UILabel and override textRectForBounds:limitedToNumberOfLines: like so:
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect newBounds = CGRectMake(bounds.origin.x + MARGIN, bounds.origin.y, bounds.size.width - MARGIN, bounds.size.height);
return [super textRectForBounds:newBounds limitedToNumberOfLines:numberOfLines];
}
Hope this helps!
Related
i have got a UILabel which is not static.
I want to resize my scroll view so that it fits the label.
Here is my idea now:
self.scrollView.contentSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
92 stands for pixels from where my label start. (there is a heading too)
But it doesn't work, it seems to be connected with Interface Builder also.
Thanks guys.
You need to set the frame as well. The contentSize of the scrollview is just what is within, not the frame of the view itself. If the contentSize is greater than the frame it will result in scrolling.
CGSize buttonSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
self.scrollView.contentSize = buttonSize;
self.scrollView.frame = CGSizeMake(0, 0, 320.0, buttonSize.width, buttonSize.height);
How do I resize the text in my UITextView so that it doesn't cut off the text? I have a UITextView set at a specific height and I want the text in that UITextView to fit in that height. In a UITextField you have a adjustSizeToWidth, but not in UITextView
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
Source: https://stackoverflow.com/a/2487402/253008
EDIT:
You can use
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
to check the width of the text in the UITextView and then when it reaches your set width you can start lowering the font size
I have a UIScrollView and I wanted the frame height to adjust proportionally when I adjust the width, is this possible? Basically I am talking about auto adjusting the frame height of the UIScrollView when I adjust the width of the UIScrollView? I have tried setting the autoResizingMask to UIViewAutoresizingFlexibleWidth and height, but this doesn't work
I don't think there's an automatic way to do it. but you can added a UIView+SCaleAddition to do this:
#interface UIView(RespectScale)
- (void)setWidthRespect:(float)w;
- (void)setHeightRespect:(float)h;
#end
#implement UIView(RespectScale)
- (void)setWidthRespect:(float)w
{
float scale = self.bounds.size.width / self.bounds.size.height;
float h = w/scale;
CGRect bounds = self.bounds
bounds.size.width = w;
bounds.size.height = h;
self.bounds = bounds;
}
- (void)setHeightRespect:(float)h
{
// write it your own
}
#end
You can do:
//get old proportions
CGFloat proportion = scrollView.frame.size.width / scrollView.frame.size.height;
CGRect frame = scrollView.frame;
frame.size.width = new_width;
frame.size.height = new_width * proportion;
scrollView.frame = frame;
I want to be align Left margin of UITextField.text to 10Px. please suggest me best way ?? same in roundedRect TesxtField where text start 10 px from left
have reached almost by overriding - (CGRect)textRectForBounds:(CGRect)bounds. now issue is when TextField goes in to edit mode their left margin reset to Zero .......
#implementation UITextField(UITextFieldCatagory)
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect theRect=CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-10, bounds.size.height);
return theRect;
}
Can you try UITextField's instance method drawTextInRect:?
I think you could use the leftView property for this.
You can add a leftView and rightView to a UITextfield. These views can be used to display an icon, but if it's an empty view it'll just take up space, which is what you want.
CGFloat leftInset = 5.0f;
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, leftInset, self.bounds.size.height)];
self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways;
[leftView release];
Refer SO question UITextField custom background view and shifting text
Have you tried
-(CGRect)editingRectForBounds(CGRect)bounds;
Override the super's implementation, and tune the results. This way you don't need to calculate with leftView and rightView (if you use them).
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect ret = [super textRectForBounds:bounds];
ret.origin.x = ret.origin.x + 10;
ret.size.width = ret.size.width - 20;
return ret;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
swift:
let leftView:UIView = UIView(frame: CGRectMake(0, 0, 30, 1))
leftView.backgroundColor = UIColor.clearColor()
my_text_field.leftView = leftView;
my_text_field.leftViewMode = UITextFieldViewMode.Always;
How would you change just the width of a UIImageView in objective-c? Currently I'm doing this:
imageview.frame = GCRectMake(x,y,width,height);
But when I change the width it seems to just change the position rather then change the size.
try this:
CGRect frame = [imageView frame];
frame.size.width = newWidth;
[imageView setFrame:frame];