Is it possible to insert Image between NSString? - iphone

I am working on a product based application in which it shows product name with price increement or decreement.
As of now, according to my requirement I am inserting a UIImage as a subview to UILabel.But its everytime I need to calculate product name length on which I am defining x position of UIImage and adding it again and again.Product name is of variable size.
Not at all sometimes x position of image could not set properly so it overlaps text on UILabel.I stucked with this problem.
Below is my effort fired on such requirement.May be there should be another way to do such things but I don't know.Any other alternative I can apply?
int level=3;
NSString *product=#"Pea Price:";
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[imgView setImage:[UIImage imageNamed:#"up.png"]];
CGRect rect=[imgView frame];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(20, 70, 200, 30)];
label.backgroundColor=[UIColor whiteColor];
CGSize size=[product sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 30) lineBreakMode:NSLineBreakByTruncatingTail];
rect.origin.x=size.width-5;
[imgView setFrame:rect];
label.text=[NSString stringWithFormat:#"%# (%i%%)",product,level];
[label addSubview:imgView];
[self.view addSubview:label];

You can use like that
label.text=[NSString stringWithFormat:#"%# ⬆\uFE0E (%i%%)",product,level];
label.text=[NSString stringWithFormat:#"%# ⬇\uFE0E (%i%%)",product,level];
just copy and paste this arrow image to NSString.
Change foreColor of label will change the arrow color.
get Other Images to paste from here
Arrows -- http://www.alanwood.net/unicode/miscellaneous_symbols_and_arrows.html
Circular Digits -- http://www.alanwood.net/unicode/enclosed_alphanumerics.html
Images -- http://www.alanwood.net/unicode/miscellaneous_symbols.html
Smiles -- http://www.alanwood.net/unicode/emoticons.html
Miscallaneous -- http://www.alanwood.net/unicode/miscellaneous-symbols-and-pictographs.html

You could use Unicode characters for arrows (⬈ and ⬊) with NSAttributedStrings for colors, or even Emojis : ↗ and ↘ (view this page in the Simulator to see them).

The simplest way to achieve this is to use a UIWebView and insert your image as an HTML tag. But performance-wise, it's not great.

Related

Using custom fonts : digital-7 fonts for Label

Did anybody used digital-7 fonts in UILabel, i found text to align in bottom(aligns too low on the Y axis instead of being in the vertical center of the label). do anybody has solution to show them center aligned?
What you're asking about, with that particular font, is that it aligns too low on the Y axis instead of being in the vertical center of the label. Some solutions:
get a font manager and fix the font by hand
adjust the UILabel instances or a UILabel subclass to push it up higher than it is. You could do this by intercepting a setFrame call, for instance.
As far as I can see, UIFont does not carry that information with it at all. It might, though: you can check some of the read-only properties of the font.
With that said, however, you still can't set properties on a font, so you cannot adjust it in this way.
try this..
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(148, 142, 130, 25)];
[testLabel setFont:[UIFont fontWithName:#"DBLCDTempBlack" size:20]];
[testLabel setText:#"MY Text"];
[testLabel setTextColor:[UIColor darkGrayColor]];
[testLabel setBackgroundColor:[UIColor clearColor]];
[testLabel setTextAlignment:UITextAlignmentCenter];
I had used custom fonts in my project earlier. Just you have to add the font.tff file in your project and set your font with setFont method

iphone : How to draw line on Label?

I want to make my label as shown in the image
I know I can get this effect by putting image view on it.
but is there any other method to do ?
How can I put line on label ?
Try this,
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = #"Hellooooooo";
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor blackColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [#"Hellooooooo" sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor blackColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
The line above will appear below the text. You just need to change Y for UIView and it'll do wonders :)
put another label with "_" over it
transparent background.
you can create UIView with line's height and width and give background color to it. Put UIView over your UILabel .
For one of my projects I've created an UILabel subclass, which supports multiline text, underline, strikeout, underline/strikeout line offset, different text alignment and different font sizes.
Please see provided link for more info and usage example.
https://github.com/GuntisTreulands/UnderLineLabel
Place a UIImageView with line image on your label so when you run application it will fit.

Custom UIAlertView iphone

I have a problem .. I used this http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color to create my own custom UIAlertView.
I do not know why but this will not work:
UILabel *theTitle = [theAlert valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
UILabel *theBody = [theAlert valueForKey:#"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];
the color of the title does not change .. the color of texbody it's ok.
How can I customize the buttons?
Hi Achieved same thing using custom UIAleartView.
Make a custom view as follows.
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 5.0f, 262.0, 49)];
tempView.backgroundColor = [UIColor clearColor];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 262.0, 49)];
lblTitle.font = [UIFont fontWithName:#"Arial" size:22.0f];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.textAlignment = UITextAlignmentCenter;
lblTitle.text = #"Subscribe";
lblTitle.backgroundColor = [UIColor clearColor];
[tempView addSubview:lblTitle];
alreadySubscriber = [[UIButton alloc] initWithFrame:CGRectMake(12, 260, 262.0, 50)];
alreadySubscriber.layer.cornerRadius = 25.0f;
[alreadySubscriber setTitle:#"Already a subscriber" forState:UIControlStateNormal];
[alreadySubscriber setBackgroundImage:[UIImage imageNamed:#"BTN0.png"] forState:UIControlStateNormal];
[alreadySubscriber setBackgroundImage:[UIImage imageNamed:#"BTN1.png"] forState:UIControlStateSelected];
[tempView addSubview:alreadySubscriber];
Insert this in UIAleartView
[self insertSubview:tempView atIndex:0];
[self setNeedsLayout];
Override layoutsubview as you have already done push all other controls down equal to view Height.
-> basically matter is to hide UIAleartView's title behind a label.
Not sure exactly what your issue might be, but we faced a similar situation when trying to workout a custom UIAlertView, so might be similar.
The custom solution in the link you provided appears to manipulate the alerts title and background by accessing the subview hierarchy and 'guessing' which subview might be which. (I may be wrong, didn't look through it in detail) The problem with this approach is that it'll work fine for one OS version, but in subsequent OS versions, Apple may restructure this subview hierarchy in some manner, and this 'guesswork' is no longer accurate. (i.e. the subview assumed to be the background image may not be).
This could be the case, seeing that posted link is an year old. If you're proceeding with this, you may have to review the subview hierarchy to see if they still match up.
I believe, that you can find proper solution without using standart tools, which are present in UIAlertView. But in this case, you application will be not approve for AppStore. That way, I strongly recommend you, avoid to using custom buttons in AlertView.
Maybe, you will find solution using UIActionSheet instead UIAlertView . It's more customizing.
If #"_titleLabel" is part of the UIAlertView hierarchy (which looks likely given the _ prefix), I can't recommend your approach.
Apple might some day change the key strings: If #"_titleLabel" ever changes, say to #"_titleLabelView", you're sunk and you might never know that you're sunk. This might even be grounds for rejection, I wouldn't know.
It's better to start out from scratch with your own custom view, subclassing UIView. Only then can you guarantee that this will be stable from OS to OS. On top of this, the time you lose trying to find a shortcut will be positively spent constructing some thing durable.

Correct way to use sizeThatFits: and sizeToFit with UILabel?

At the moment I have a label being sized correctly using [aString sizeWithFont:constrainedToSize:lineBreakMode:] but I've introduced rotation into my device and with a flexible width and height this results in my UILabel stretching width ways (because its set relative to the main view) but not then contracting height-wise to compensate for the extra space.
In another question I asked I was told to use sizeToFit and/or sizeThatFits: however I can't find any useful resource on the internet that tells me how to use this appropriately and trials I've done result in irrational behavior.
In a nutshell, lets say I have a structure like this:
UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0)];
[innerView setAutoresizingMask:*flexible width and height*]
[innerView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:innerView];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, innerView.frame.size.width-20, 0)];
[myLabel setAutoresizingMask:*flexible width and height*]
[myLabel setBackgroundColor:[UIColor blueColor]];
[myLabel setNumberOfLines:0];
[myLabel setLineBreakMode:UILineBreakModeWordWrap];
[innerView addSubview:myLabel];
Thats a simplified version (also by way of disclaimer, this isn't my acctual code, I realise these elements will leak and everything else... this is just an example so you can see the structure).
Basically I have those two elements. Now on rotation they will both stretch to fit the new view size. What I need is a way to increase or decrease the height to fit the content dynamically. [aString sizeWithFont...] is only static. So how would I use sizeToFit or sizeThatFits to make this dynamic?
Thank you.
Tom
Have you tried resetting the frame of your labels after the screen rotation? Check the value of interfaceOrientation in:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Then, set the label's frame accordingly for each view mode inside didRotateFromInterfaceOrientation.
Here's an example of resizing label frames for string length: http://cocoamatic.blogspot.com/2010/08/uilabel-dynamic-sizing-based-on-string.html

Big activity indicator on iPhone

Does anyone know how to show a rounded squared with a spinning activity indicator? It is used in many apps. If you don't know what im talking about, it looks like the indicator when you change volume on your Mac but with a darker background. Im not sure if it is built-in to iOS or someone made it.
Like the one in this post but not full screen just the activity indicator
How to create a full-screen modal status display on iPhone?
Here's what I use when I want to show that kind of indicators.
UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)];
loading.layer.cornerRadius = 15;
loading.opaque = NO;
loading.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
UILabel *loadLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 25, 81, 22)];
loadLabel.text = #"Loading";
loadLabel.font = [UIFont boldSystemFontOfSize:18.0f];
loadLabel.textAlignment = UITextAlignmentCenter;
loadLabel.textColor = [UIColor colorWithWhite:1.0f alpha:1.0f];
loadLabel.backgroundColor = [UIColor clearColor];
[loading addSubview:loadLabel];
[loadLabel release];
UIActivityIndicatorView *spinning = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinning.frame = CGRectMake(42, 54, 37, 37);
[spinning startAnimating];
[loading addSubview:spinning];
[spinning release];
loading.frame = CGRectMake(100, 200, 120, 120);
Then you just add the 'loading' view to the view of your choice and you got it.
Hope this is what you needed.
Your screenshot is probably a usage of David Sinclair's DSActivityView module. Specifically, the DSBezelActivityView component of it. Or if not, it's a close copy.
http://www.dejal.com/developer/dsactivityview
I use DSActivityView all the time. Great library. Toss that thing up while pulling down data, keeps users and clients happy.
One option: MBProgressHUD.
I don't think that screenshot is my DSBezelActivityView; the metrics look a little different. But it is very similar.
Note, DSActivityView and its subclasses don't use any images or nibs; they're pure code.
To answer the original question, it'd be easy to modify DSBezelActivityView to omit the fullscreen gray background. You could do it by subclassing and overriding the -setupBackground method thusly:
- (void)setupBackground;
{
[super setupBackground];
self.backgroundColor = [UIColor clearColor];
}
Hope this helps!
Try this simple method, Its working well for me....
UIActivityIndicatorView *activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
activityIndicator.layer.cornerRadius = 05;
activityIndicator.opaque = NO;
activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
activityIndicator.center = self.view.center;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activityIndicator setColor:[UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]];
[self.view addSubview: activityIndicator];
You're actually looking at using two UIView subclasses and a custom .png image to get the look you want.
The Gray translucent box would be a UIImageView object, to get the effect you're looking for you need a .png file of a grey square with rounded corners, it doesn't need to be the final size, as long as there's at least one pixel of straight edge between the corners it will work fine. You'll then load it in as a UIImage with the UIImage
stretchableImageWithLeftCapWidth:topCapHeight: method, this let's you specify the top, and left portions of the image that must stay the same, and a 1 pixel slice in each direction will be stretched out to fill the UIImage view you use the image in. http://www.bit-101.com/blog/?p=2275 has a great example of how this works.
So create a UIImage, then create a UIImageView using this image, set its opaque property to NO and the alpha property to something that looks good to you. Add this a subview of your current view.
Now you just need to add the spinning progress indicator, this is even easier, just create a new UIActivityIndicatorView and add it as a subview of the UIImageView you've already created.
The same basic method is used to create pretty much any resizable element in an iOS application. There's some examples of using them for buttons in Apple's UICatalog example code.