iOS 14 Pickerview Cutting Off Text

Preventing the labels to be cutting off from the sides - TagsView

You are almost there.


Every Texts have 16 points padding on each of their sides. And the VStack has 16 points padding too. You need to consider them when calculating the width of the label.

let viewWidth = label.frame.size.width + 32

print("viewWidth = \(viewWidth)")
if (width + viewWidth + 32) < screenWidth {
width += viewWidth
print("width = \(width)")
tempItems.append(word)
}


You may calculate the spacing between every two Texts too.

SwiftUI iOS 14 Picker width can't be changed

The width: 10 is too small... anyway, use .clipped (or .clipShape) to restrict drawing outside bounds

demo

Picker(selection: self.$position, label: Text("")){
ForEach(0..<50){ i in
Text("\(i)")
}
}
.frame(width: 50, height: 150, alignment: .center)
.clipped() // << here !!

UIDatePicker Cutting Off Text & Not Displaying Days

Ok, I figured it out. This happened as a result of trying to use UIAppearance on a tableView background color. This has nothing to do with tableViews on the face of it, but Apple must be using a tableView privately for the PickerViews. So, my attempt to set a UIAppearance via a category on a tableView background color seems to be doing something unexpected. Lesson learned. Don't try to use UIAppearance where they are not officially supported.

How to change UIPickerView width with multiple Pickers in SwiftUI?

You need to remove compression resistance priority

let picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

and then can use any frame as you need in SwiftUI part (static or calculated)

demo

    MainPicker(pickerSelections: self.$selections)
.frame(width: 200)

Demo prepared & tested with Xcode 11.7 / iOS 13.7

Maximum lineheight cutting off text

After spending a few hours in the torture chamber I finally figured it out! The issue was the font itself. It was an .otf and not a .ttf. Because of this swift did not understand the font's internal lineheight and just cut it off. After switching to .ttf the issue was resolved

Sample Image

CGImageCreateWithImageInRect cutting off

CGImageCreateWithImageInRect(screenshot.CGImage, crop) does indeed return a cropped version. Here is the relevant comment from the documentation:

Quartz performs these tasks to create the subimage:

  • Adjusts the area specified by the rect parameter to integral bounds by calling the function CGRectIntegral.

  • Intersects the result with a rectangle whose origin is (0,0) and size is equal to the size of the image specified by the image parameter.

  • References the pixels within the resulting rectangle, treating the first pixel within the rectangle as the origin of the sub image.

A simple way to solve this is to adjust the position of the resulting UIImageView so it is correct for its size. Here is the relevant calculation:

let screenshotBounds = CGRectMake(0.0, 0.0, screenshot.size.width, screenshot.size.height)
let cropIntegral = CGRectIntegral(crop)
let cropIntersection = CGRectIntersection(cropIntegral, screenshotBounds)
bgImage.center = CGPoint(CGRectGetMidX(cropIntersection), CGRectGetMidY(cropIntersection))

cropIntersection is the bounding rectangle of our extracted image (following the first two steps given in the documentation). Therefore, we can use it to position the imageView to the same place in the original image.

How to change the Font size in UIPickerView?

You need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
}
// Fill the label text here
...
return tView;
}


Related Topics



Leave a reply



Submit