I'm trying to shift the text in my UITextView right by 20 pixels to create a text indent using the code below
[textview setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)];
But this is cutting off the right hand side of my text as shown in the screenshot. Can any help me stop this happening please
I think your edge insets are a bit off. Edge insets work towards the middle of the rectangle. For the right inset you should try 20 rather than -20.
[textview setContentInset:UIEdgeInsetsMake(0, 20, 0, 20)];
I couldn't find a solution to your problem even by subclassing UITextView and blocking setContentSize which seemed to be the problem.
[textview setContentInset:UIEdgeInsetsMake(0, 20, 0, rightInset)];
No matter what is the value of the rightInset the text is always clipped. Something is always redrawing text so that it fits the frame width of the text view.
But I am wondering why do you need to set the inset? Can't you just set the background of the UITextView object to clear color and position it properly on your background view to get the illusion of indentation?
Related
I want to create a close button which will look like a circle with an x in the middle. In x code I set the button's size and width to be large so that the touchable area is larger (50 x 50 with a font of just 22).
I create a button, change the title to X and then set the following:
[self.closeButton.layer setBorderWidth:2.0f];
[self.closeButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[self.closeButton.layer setCornerRadius:self.closeButton.bounds.size.width/2];
The circle border is too far from the X. How would I bring the borders in tighter to the X but not decrease the size of the clickable area?
assign image to a button. and make tappable area as you want. then set image location in button by setting inset value of that button as top, bottom, left, right. change the value of this according to your requirements. You can set inset value from interface builder as shown in image here. change the value and see the difference to place your image in button at exact location you want.
exactly like this image
I'd suggest exactly what Max has done. He has shown how to do this via IB, this is how you do it through code:
myButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 25, 25);// UIButton
myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 25, 25);// UIBarButtonItem
EDIT:
Sorry for not reading your question properly the first time. I think you will run into the same issue with this approach : the border/cornerRadius of the button will not respect any insets. It will be drawn according to the frame of the button. Only (easy) workaround at the top of my head, is to create an image with the border and corners in it, and then set it as the image. The image will respect the insets, and you will have your desired tappable area, with the borders exactly where you want them.
There might be a more elegant workaround through subclassing, but unless you change the border width/color, or corner radius of your button at any stage, I'd suggest sticking with custom image.
EDIT 2:
Instead of using an image you might want to use this unicode character.
I have editable UITextView that expands as I start writing into multiple rows. At some point the height of UITextView reaches predefined maximum height and becomes scrollable. While UITextView was not scrollable everything looked nice.
When text view becomes scrollable, weird thing starts happening with spellcheck underlining red dots. Details on attached image.
Can anybody help me with this? I'm sure someone has encountered it so far.
It is clearly a bug that was introduced in iOS 7 (still there for 7.0.6).
As soon as you make UITextView scrollable, spellchecker underlines are getting mashed. If you set
textView.scrollEnabled = NO;
then it's fixed.
Solution is to put your text view into scroll view and resize scroll view's content size when text grows.
Here are some screenshots. I've also made views coloured so that you can see the bounds of all text view's subviews. Interesting that with .scrollEnabled set to YES text is inserted in some other UIView which height is being calculated wrong.
.scrollEnabled == YES
.scrollEnabled == NO
I have a UITextView with a width of 240. Monitoring the text width shows that it is wrapping the text when the text width >= 224.
In this situation, where the UITextView wraps the text onto the next line, but the length of the text is not greater than 240, the text height is being reported as if it was a single line of text, rather than the height of two lines (since the UITextView wrapped it).
Any ideas why this is happening and how i can resolve it?
UITextView is a subclass of UIScrollView and there are some default insets applied—8 pixels on both the left and right side. I have never managed to find a way to get rid of them.
When using resizableImageWithCapInsets: to create an image for a UIButton only the normal state (the state set the image with using setBackgroundImage:forState:) works. All other states show a gap instead of the drawn image. UIButton says that if no image is set for a particular state, the normal state image will be used with an overlay for disabled and selected states.
Here is the normal state:
Here is the selected state:
And here is the source image:
It clearly is using the resizable image I provided, but the image is not drawing the resized area. (You can see the left and right edges but the middle area that is to be stretched just isn't drawn).
Interestingly, stretchableImageWithLeftCapWidth:topCapHeight: does work. Now this is a deprecated method in iOS 5, but with the gap being shown in the new API, I may be stuck using it.
I do recognize that I can provide more images for each state but that defeats the purpose I'm trying to achieve of reducing memory footprint plus adds extra dependency on my graphics designer which I'd like to avoid.
// This is the gist of the code being used
UIImage* image = [UIImage imageNamed:#"button.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2);
image = [image resizableImageWithCapInsets:insets];
[self.button setBackgroundImage:image forState:UIControlStateNormal];
// Even doing the following results in the same behaviour
[self.button setBackgroundImage:image forState:UIControlStateSelected];
You aren't creating your insets properly for the image capping. I've reproduced your issue and corrected it by using the correct insets.
With your current code, you are creating caps of half of the image height and width - this leaves you with a "stretchable" area of 0x0 pixels - so you get nothing in the middle.
Why this isn't showing up as wrong in the normal state of the button I'm not sure - perhaps there is some optimisation built in to UIButton to fix things or auto-strectch if you don't supply a stretchable image, and this is not applied to the other states.
The caps are supposed to define the area of the image that must not be stretched. In the case of your button.png image, this is 6 pixels on the left and right sides, and 16 pixels in from the top and bottom. This isn't quite standard, you should tell your graphics designer that (at least for left-right which is the most common stretching) you should only have a 1px area in the centre, however this does not affect the outcome. If you do have a 1px stretchable area then you can standardise your code by deriving the caps from the image size as you have tried to do in your question (each cap is then (image.size.height - 1) / 2 for top/bottom, same but with width for left/right).
To get the correct images on your button, use the following code for creating the stretchable image:
UIEdgeInsets insets = UIEdgeInsetsMake(16, 6, 16, 6);
image = [image resizableImageWithCapInsets:insets];
I was experiencing problems while using resizable images on iOS5 too. It turns out that if your button is of type "RountedRect" and you manipulate the background images, the resizable images will not behave as expected. (iOS6 handles this ok, presumably by assuming your new button type and adjusting as needed.)
I need to draw an effect like this on a UILabel.
How can I do it?
U can try the below code. U can change the color of the text or the shadow based on the color of the background.
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
If you're on gray background, I usually just use UILabel's text shadow property positioned one pixel below with color white. That comes quite close. If you really need more (i.e. if you want to have the dark edges above, then you need to write a custom label that draws the text multiple times). CoreGraphics can help you a bit with the CGContextSetShadow, though.