UILabel text scaling, Programmatically? - iphone

I want to change my UILabel text or UILabel itself's size by using UISlider, I know it's something we don't do it for an normal application. But basically I am trying new and different things to explore Objective -C language and iOS platform.
So far I am able to change colour of a font by UISlider, Which looks really great, So thought of changing it's size too by UISlider. Below I am posting pictures to clarify my thoughts visually!
From ----->
To ------->
While making those images I realised I might have to change size of UILable and Size of font too. Am I right? Is this possible?

textLabel.font = [UIFont boldSystemFontOfSize:slider.value];
CGSize newSize = [textLabel.text sizeWithFont:textLabel.font];
This should make it
an alternative is to set the fram of the label above the complete size above the textField and set textAlignement = center

its quite simple actually
According to slider change value dynamically increase height and width of your label..

There can be many ways you can handle this, one way would be to just change the font size with slider and when you change the font calculate the frame size with sizeWithFont: instance method of NSString.

Related

Large width of UILabel Cause memory issue

I want some marquee label type control in my application. I have created it but the problem is as below:
1.I have setup the scrollview first and then in that scrollview i have added UILabel with large text around 7000 character.and i am shifting content offset of the UIScrollView to make a text scrolling.
Now when allocating label, i make the width of the label according to the stringsize width like below:
UIFont *font = autoScrollLabel.font;
CGSize stringSize = [str1 sizeWithFont:font];
float width = stringSize.width;
and i give this width (its around 250000) to the UILabel width Like below:
autoScrollLabel.frame = CGRectMake(15,25,width,98);
=====
Now the problem is if i am allocating this width to UILabel with then it occupy around 300MB size of the application and at some instant it crashed the application due to memory issue.
Can anyone had ever faced this issue then please help on how to solve this memory issue or anyone who have any idea please put you suggestion.
I have also other functionality like play, pause, scroll text with animation, and move the text offset according to the UISlider and start it from there.
Thanks
A UILabel will render all the text at once event if it is off the screen. This is what is causing your memory issue.
As an alternative, could you use a UITextView in your code to achieve the same functionality?
UITextView inherits from UIScrollView so you can set the textview's frame to bounds of the superview but adjust the contentSize property to stringSize
Edit: Sorry, the above wouldn't work as you cannot control the way the UITextView renders its text.
How about trying this: https://github.com/cbpowell/MarqueeLabel

CGAffineTransformMakeScale on UITextView

I have an instance of UITextView. It is actually a box, and when user taps on it, I want to make it bigger. So I implemented CGAffineTransformMakeScale in tap event.
The problem is, the text inside the UITextView gets fuzzy and blurry. I set contentMode = UIViewContentModeRedraw, but didn't affect it.
Any idea? Many thanks in advance.
This is always going to happen. If you scale it like that using the transform it will always get blurry because the whole textview is scaled, including all contents.
The only way to prevent this is not using the transform scale but simply make the textview frame bigger!
Can you try to reset the text after making the font bigger on the tap?
say
NSString *placeholderText = textView.text;
textView.font = // bigger font proportional to scaling
textView.text = placeholderText;

How to draw bullet in table views cell?

I want to draw bullet in my table views cell.
I found one method for getting bullet is :
lbl.text = #"\u2022 Hey its bullet";
But it is giving small bullet.
So is there any other way to get bullet in my apps?
If the bullet is small it's because that the standard size for the font-family/font-size you are using.
You'd have to set the bullet font to a different font family or font size to increase it's size.
I assume you're using a default cell view. You could create a custom cell view and manually place a bullet in interface builder with a UILabel after it.
You could try adding an image to the imageView property of your tableviewcell. Something like this:
myCell.imageView.image = [UIImage imageNamed:#"myBulletPoint.png"];
You'd probably want to cache the image, but this should get you going.

whats the height of uitextfield in xcode (iphone)

the square kind... (not rounded)
Im trying to make a background that fits my texfield and cant seem to find the height (default) anywhere
Default height is 31pts (you can see that in IB), but it is fixed only for rounded border style - with all other styles you can set field height to arbitrary value.
btw, you can find a reference for common UI elements sizes here
[textField sizeThatFits:textField.frame.size].height
If you hard-code 31pts then you will have unsightly visual errors if (when) Apple ever changes the size.
For iOS and iPadOS 14.5. The default height is 34 points for the rounded border style as seen in the Interface Builder.
However, this value can change. To check the size use the following code snippet.
let textField = UITextField()
textField.borderStyle = .roundedRect
print (textField.sizeThatFits(textField.frame.size).height)
Output
34.0
UITextField is a subclass of UIView so you can send -frame or -bounds to the object.

How to set height for a textfield in size inspector

i created a UITextField of type rounded textfield, using Interface Builder,so in order to set the Height of a textfield its window was disabled(not to set any height manually) in SizeInspector.so if we want to set height manually,what procedure should we follow.
One thing i would like to mention is ,we can do that in coding part,but what i would like to know is ,how can we acheive that in interfacebuilder.
Answers from you were always appreciated.
Change the border style, like this:
Unfortunately you can't set the Height of a UITextField using Interface Builder. You will have to set it programatically and be sure it doesn't break any rules from iPhone Human Interface Guidelines
You can change the height of UITextField by editing the .xib file. Just open it using Textedit or Dashcode and search for the IBUITextField. Edit to NSFrame parameter to whatever size u want.
increase the size of the font and the box's height will increase
Change the Border Style property of the text field to something other than the rounded rectangle. After that you can freely set the height of the box.
Right click on .xib file and click on "Open As"-> "Source Code". Then search for UITextField's NSFrame. Change the height as you want.
Successful solution with my condition:
After trying all above ways, I could not change the height of UITextField.
Finally, I found out that I put UITextField in stack view (or it was affected by some constraints).
So, I added Height Constraint for UITextField then put my expected height in Constant of Constraint.
And it works.
Specially, it works for all Border-style (None, Line, Bezel, RoundedRect).
No code is needed.
After a bit of playing around, I realized that UITextField can be thought of as a UIButton (Rounded rect style) with a UILabel on top of it. Or, it is a UITextView on top of UIButton Rounded rect style. The number of lines, dimensions and position of the label inside the button can be easily adjusted in IB.
In my case, I wanted a read only text-field so I used UILabel on top of UIButton. I adjusted the size of button as per requirement and also label size. I also had to uncheck the 'Enable' box for the button in IB so that it does not get highlighted on touching.
You can easily change that using source code. Just right click on your storyboard, open it as source code, then search for your text field and change the height.
The easiest way that i have found to accomplish this is to change the border style and then edit the other attributes to your liking. Here's an example.
emailField.layer.borderColor = [[UIColor blackColor] CGColor];
emailField.layer.borderWidth = 2.0f;
emailField.layer.cornerRadius = 8.0f;
emailField.alpha = .75f;
emailField.layer.backgroundColor = [[UIColor whiteColor] CGColor];
!
I resolved textfield (with rounded rect) height problem. You must define height constraint for your textfield object.
Define height constraint for Textfield.
We can change height of Text Field with any type of border style other than rounded rectangle by simply dragging. But for the Rounded Rectangle Text fields it is not possible...of course we can change height using some code.
And here I found a simple way. It worked for me
Select the text field and at the below right corner we can see the pins option..
There select the height constrain it is 30 pre-defined...
select that height and you can see "Add 1 Constraint" is enabled.
Click on that..therefore we have added height constraint for that text field.
Now go to the size inspector. There find the Height constraint in "Constraints"...which is above the "Content Hugging Priority"
You can edit the height there...give whatever value you want to give. Now see the textfield it is changed to its height which you have given.
It worked for me :) Can use this scenario for UISwitch, Segment, etc..
As #peterdoesco.de said, Just add an Height constraint, and change it's value, this can works fine.