I have a dynamic multi line UILabel and need to know the end of the text (X Coordinate) of the visible text (not the Label) so I can show something after the text.
Is this possible?
thank you
You'll be able to have more control over the text layout with the CoreText framework. Checkout the documentation:
There are also some nice open source things that already do a lot of the hard work for you, like: https://github.com/Cocoanetics/DTCoreText
As far as I know , the best solution for this is to use a UIWebView instead of a UILabel.
You just have to format the HTML for it to load and then add whatever you want to add after.
Example:
[webView loadHTMLString:[NSString stringWithFormat:#"<html><body><font face=\"arial\" size=\"2\">%#</font><font face=\"arial\" size=\"1\"> %#</font></body></html>",text1 , text2] baseURL:nil];
If you want to keep trying with UILabel / UITextView /any UIView for that matter , I only know of a way to figure out the height properly : [myView sizeToFit]; And then get it's frame.
Hope this helps.
Regards,
George
Related
I'm wanting to create a read only text area in my app which allows the user to click on any word and the app reads it out. I am however a little confused on which method would be the best. I see two options, use a UILabel and create some method to detect the region clicked then match it to the word in that region but it sounds hard to implement. On the other hand I could use an array of words to create a list of UIbutton's. Any advice and/or sample code to help me would be much appreciated, thanks Jason.
Note: Each view has about 30 words on it.
The solution below works well. For anyone else wanting to use this, these four lines will set your UIWebView to have a clear background and disable any scrolling or bounce.
[[myWebView.subviews objectAtIndex:0] setScrollEnabled:NO];
[[myWebView.subviews objectAtIndex:0] setBounces:NO];
[myWebView setBackgroundColor:[UIColor clearColor]];
[myWebView setOpaque:NO];
And some handy css to stop the open popup when a user presses and holds a link.
*{-webkit-touch-callout:none; -webkit-user-select: none;}
How big is your text area? If it's big then creating a UIButton for each work sounds like sa bit of effor to get the text to layout correctly.
I would use a UIWebView - make each word like this :
WORD1 WORD2 WORD3
and attach your view controller as the webView's UIWebViewDelegate delegate.
Then, you can intercept presses on each word using the webView:shouldStartLoadWithRequest:navigationType: delegate method :)
In my app, I have a textbox that the user can fill. Most times, this will be quite a lenghty text, I'm using UITextField but this does not go to a new line when the user types over the iPhone screen. How do I make it go to the next line?
Just Go With UITextView instead of UITextField.
UITextField doesn't provide any option to make it multi-lined text box. So you have to go with UITextView.
In interface builder place an UITextView and connect with an IBOutlet. I am assuming that you know how to do that.
Suppose you connected this UITextView with an IBOutlet named address. So now write the following code on viewDidLoadmethod.
- (void)viewDidLoad
{
[super viewDidLoad];
address.layer.cornerRadius = 5;
[address.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[address.layer setBorderWidth:2.0];
address.clipsToBounds = YES;
}
Now add this line at the beggening of the file to import QuarzCore library
#import <QuartzCore/QuartzCore.h>
Now your TextView will look like a TextFiled.
Hope it will work for you.
I'm not sure , but maybe this could help you :
- Go to the interface builder
- On the text field attributes , change the border to your type
- Some border are made so you can change the size and all
Hope it helps !
does anyone know of a way I can change the text label for on and off to yes and no.
I did it with
((UILabel *)[[[[[[switchControl subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = #"Yes";
((UILabel *)[[[[[[switchControl subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = #"No";
However, with the release of iOS 4.2, this is no longer supported (this probably wasn't recommended by Apple anyway)
My client is insisting on yes/no switches. I'd appreciate any advice!
many thanks
Hurrah! From iOS 6, it's possible to specify an image to be used for the on / off states, respectively. So, this can be used to display a YES / NO image (or whatever image representing the text you would prefer to use instead of the previously limited ON / OFF).
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
{
[mySwitch setOnImage: [UIImage imageNamed:#"UISwitch-Yes"]];
[mySwitch setOffImage:[UIImage imageNamed:#"UISwitch-No"]];
}
The images should be 77 px wide, 27 px high, and the text (one image for each state) should be horizontally centred within that 77 px width. I use transparent backgrounds for the text, so I can still make use of the tint for the background, which still works with this.
Of course, it would seem easier to just supply text, rather than having to use an image of text, but I'm certainly grateful for this new option, at least.
You need to implement your custom UISwitch for that. Or use one of already implemented :) (check this SO question and this post)
Vladimir answer is great, but in my humble opinion there is an even better implementation here: https://github.com/domesticcatsoftware/DCRoundSwitch.
Besides setting a custom text, it is easier to change the size and color of the UISwitch and you get a sharper result.
It is released under an MIT license. Have a look!
It turns out that you can create a custom UISwitch with the following items:
A UIScrollView
A UIButton
Two UILabels
A background image
A Boolean value
First you will need to add QuartzCore.framework to your project and #import <QuartzCore/QuartzCore.h> to your view controller.
Next add the UIScrollView to your view using Interface Builder. The ScrollView will be your custom UISwitch.
Next add the button and the two labels to your ScrollView. One label will be for "yes" the other for "no".
Add the image to the button and set its type to custom. This is the image I use:
Position the labels over the blue and white area of the image. Adjust the ScrollView so it is just big enough to show the blue part of the image and the thumb nob.
Add the following line to viewDidLoad:
self.mySwitch.layer.cornerRadius = 13.5;
mySwitch is the name of the ScrollView and 13.5 is half the height of the ScrollView. The above statement changes the ScrollView to have rounded ends like the UISwitch.
To make the custom switch active you will need to tie the buttons "Touch Up Inside" event to an IBAction. Here is the code I use in the event handler:
-(IBAction)mySwitchButton:(id)sender {
self.myValue = !self.myValue;
CGPoint scrollPoint = CGPointMake((self.myValue)? 43.0: 0, 0.0);
[mySwitch setContentOffset:scrollPoint animated:YES];
}
Where myValue is the boolean variable that contains the state of your switch and 43.0 is the number of points you will have to move the image over to put the switch in the off position.
That is all there is to it!
From iOS 6, it's possible to specify an image to be used for the UISwitch on / off states, but NOT the text.
This will lead trouble when internationalization is required because translators
have to provide an image text for each language, not text only.
Moreover, the size of the UISwitch image is fixed, limiting the text length.
Because of the above reasons, I like the JSWilson's answer: simple and flexible.
To relieve developers of the need to manually add the required controls, I coded a custom CRDScrollSwitch class that you can find at my GitHub repository:
https://github.com/corerd/CRDScrollSwitch
I have been able to set the color of the background of my UIWebView, but not the textcolor.
How can I set the text color?
I use this code
itemSummary.textColor = [UIColor colorWithRed:202 green:192 blue:146 alpha:0.75];
but get an
request for member 'textColor' in something not a structure or union error.
Thanks
You can set the text color in HTML shown on UIWebView.
UIWebView doesn't have a method textColor. The only classes that I can see that do are: UITextField, UITableViewCell, UILabel and UITextField.
Because UIWebView inherits from UIView you can set the background color. To change the text you'll probably have to change the html of the page you're viewing.
You can set the color using standard css style as suggested here i.e.:
[self.description loadHTMLString:[NSString stringWithFormat:#"<html><body p style='color:red' text=\"#FFFFFF\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%#</body></html>", justifiedString] baseURL: nil];
Hope it helps you and it worked perfect for me,thanks :)
I made a method that wraps some text in a HTML body with the correct style for a given UIColor and UIFont.
Simply pop the generated HTML into the webview:
NSString * htmlString = [MyClass htmlFromBodyString:#"an <b>example</b>"
textFont:[UIFont systemFontOfSize:10]
textColor:[UIColor grayColor]];
[webView loadHTMLString:htmlString baseURL:nil];
you can not set the text color in UIWebView directly.
set text color in your HTML file load new HTML file in UIWebView.
im working on a editable notebook type project. it consists some text and images at any time.
in UITextView if we add images as subview the frames are fixed. but i have editable option. so i must save image as NSString format in UITextView, but it should look image type in uipart. so please suggest me how can i handle this requierment.
Thanks in advance
I think you're asking for something that's not possible.
If you need to truly interleave text and graphics, UIWebView is about your only answer. But it's not editable.
I know it's too late.
As per my understanding, you can use UIWebView to insert text and images both. Create an HTML file and put it in app bundle. Set the attribute of div or paragraph(in HTML) as contentEditable = "true". And load that file:-
NSBundle *bundle = [NSBundle mainBundle];
NSURL *indexFileURL = [bundle URLForResource:#"Index" withExtension:#"html"]; // HTML file name is "Index"
[YourWebView loadRequest:[NSURLRequest requestWithURL:indexFileURL]];
This way, your UIWebView becomes editable and you can insert text and image both. Edit:- Please have a look at this link:- Adding Images to UITextView
Solved as follows. Taken Scrollview, in that managing size of the textview & inserting image with Specified size. Like textview,image,tetxview, image and goes on...
Thanks for all your support to solve the issue.