Detect textfield overflowing - swift

I'd like to be able to detect when a UITextfield overflows it bounds.
I want to detect Horizontal overflow (since there is only 1 line).
I was thinking about counting the amount of characters and multiplying that with a default value for the letter width and see if that fits inside the textfield bounds but that wouldn't work due too the letter having different widths.
I am aware of adjustsFontSizeToFitWidth but I want to increase the width of the textfield instead of decreasing the font size.

Here is the code which first calculates the size of the text and then compares it to the label size:
let textSize: CGSize = self.label.text!.sizeWithAttributes([NSFontAttributeName: self.label.font])
let isOverflowing: Bool = textSize.width > self.label.frame.size.width

Related

Tabelview rows not showing(because of wrong constraints) in swift

this is the cell design design img
for pic
width = height = 40, top = 15, leading = 15
here for name rose d i have given constraint
width = 60, top = 15, leading = 10
for place a bid on the request label
top = 15, leading = 5
for trip for few days label
top = 15, leading = 5, trailing >= 20
for dummy caption shoe here label
trailing = 20, leading to image = 10, top to rose d = 5 and label lines = 0
for 46 mins ago label
top and leading to dummy caption.. lable = 10, trailing >= 20
i don't want to give row fixed height.. because dummy caption label may increase..
now rows not showing in tableview.. only if i give fixed height then only showing
here where i go wrong with constraints.. so i not getting rows in tableview, please guide me
if i give bottom constraint to lowest label and if i add more text to middle label then coming like this design and constraint of middle label img
You have not set any bottom constraints, therefore the layout doesn't know how to anchor the bottom of the cell to your UI elements.
Set constraints from the lowest element to the bottom of the cell's content view, or more flexibly set greater or equal to constraints from all lower elements so you don't need to worry about the number of lines in your labels.
However, I think a better approach would be to use a single label with an attributed string to provide the colouring. This will be more flexible and ensure the spacing between words looks natural.

How to adapt crossAxisCount of GridView based on its width in Flutter?

What is the best practice to adapt the number of columns (crossAxisCount) of a GridView based on its width in Flutter?
Maybe I can better explain the intended behaviour by referencing HTML:
I create 6 boxes (e.g. DIVs) with a width of 200 px (and same height) each.
(a) If the element surrounding these boxes has a width of 400 px, it would automatically show 2 boxes per row in 3 rows.
(b) If the element surrounding these boxes has a width of 700 px, it would automatically show 3 boxes per row in 2 rows.
Use Wrap. It can put renderboxes as many as fit to a row, then wrap to the next row. (formerly a comment, but it looks like the answer!)
I do this like below. You can try..
int getItemCountPerRow(BuildContext context) {
double minTileWidth = 200; //in your case
double availableWidth = MediaQuery.of(context).size.width;
int i = availableWidth ~/ minTileWidth;
return i;
}

Is it possible to set line height less than 1 in Swift?

I am trying to make the line space a little less than the default for a small window.
I have code similar to this question: How to Increase Line spacing in UILabel in Swift
let title = "This is text that will be long enough to form two lines"
let styles = NSMutableParagraphStyle()
styles.lineSpacing = 0.1
let attribs = [
NSAttributedString.Key.paragraphStyle:styles
]
let attrString:NSAttributedString = NSAttributedString.init(string: title, attributes: attribs)
introText.attributedStringValue = attrString
While changing lineSpacing to 10 makes a noticeable difference, I can't see a difference if I make it less than 1.
Here is what 0.1 looks like:
Line spacing is measured in points, not lines. There's basically no such thing as a fraction of a point (for drawing purposes; I am simplifying, since retina screens do exist). Zero is the minimum, and when you say 0.1, you are there; you can't reduce the leading any further.
Keep in mind the relationship of points to pixels. For most recent devices, a point represents two or three pixels. I have set a UIView with a height of 0.5, used as a divider, which is about 1 pixel on many devices, and been able to see the difference. A height of 0.1 is probably rounded off to nothing, though.

How to shrink the size of the text inside a label

Im trying to get text to fill the entire bounds of a label no matter how long or short the text string is. I want the largest possible font size without any truncation or clipping.
I set up my label like so:
var messageTitle = TTTAttributedLabel()
messageTitle.setTranslatesAutoresizingMaskIntoConstraints(false)
messageTitle.font = AppTheme.largeMessageFont()
messageTitle.verticalAlignment = .Bottom
messageTitle.numberOfLines = 0
messageTitle.adjustsFontSizeToFitWidth = true
messageTitle.minimumScaleFactor = 0.2
And set the various constraints to set the size of the label to be 250 x 250.
I'm pretty sure this used to work. The label text now gets truncated if it is long when it should be shrinking the size of the text
You can set the font into maximum font size then add this attribut
theLabel.adjustsFontSizeToFitWidth = true
theLabel.minimumScaleFactor = 0.5 //this is the minimum scale factor, set it as small as you want

How can I determine the area currently visible in a scrollview and determine the center?

I have a map app where the user can place waypoints manually. I would like for them to press the waypoint button and have a waypoint placed in the center of their currently visible view on the content view.
I'm afraid you'd have to calculate it yourself. contentSize returns size of the scrolled content, contentOffset gives you the origin of the scroll view inside the content. Then with scrollView.bounds.size you can find the center of the view.
Haven't tested this, but maybe you could convert scrollView.center to your scrolled map like this:
CGPoint viewportCenterInMapCoords =
[scrollView.superview convertPoint:scrollView.center
toView:mapViewInsideScrollView];
Need to account for how zoomed it is, then I can convert the content offset to the size of the full image and add some.
/// this is the full size of the map image
CGSize fullSize = CGPointMake(13900, 8400);
/// determines how the current content size compares to the full size
float zoomFactor = size.width/self.contentSize.width;
/// apply the zoom factor to the content offset , this basically upscales
/// the content offset to apply to the dimensions of the full size map
float newContentOffsetX = self.contentOffset.x*zoomFactor + (self.bounds.size.width/2) *zoomFactor-300;
float newContentOffsetY = self.contentOffset.y*zoomFactor + (self.bounds.size.height/2) * zoomFactor-300;
/// not sure why i needed to subtract the 300, but the formula wasn't putting
/// the point in the exact center, subtracting 300 put it there in all situations though
CGPoint point = CGPointMake(newContentOffsetX,newContentOffsetY );