UITextView border for each line - ios

I'm developing an application, which has textView with bottom borders for each line, I can't find any answers on this question on google and on StackOverflow. So i decided i'll ask a question, i have this textview:
and i want it to look like this:
I'm doing it programmatically so please don't give me storyboard examples, Thank you

var textview:UITextView=UITextView()
for var i:Int = 20 ; i <= Int(textview.frame.size.height) ; i = i + 20 // set 20 you line distance .. change your chooice
{
let border = CALayer()
border.borderColor = UIColor.grayColor().CGColor
border.frame = CGRectMake(0, CGFloat(i), textview.frame.size.width*1.5 , 1.0)
border.borderWidth = 1.0
textview.layer.addSublayer(border)
textview.layer.masksToBounds = true
}

You Can Use this code
-(void)setBorderView:(UITextField*)textField
{
UIView *borderView = [[UIView alloc]init];
borderView.backgroundColor = [UIColor clearColor];
CGRect frameRectEmail=textField.frame;
NSLogVariable(textField);
borderView.layer.borderWidth=1;
borderView.layer.borderColor=[customColor colorWithHexString:#"8c8b90"].CGColor;
borderView.layer.cornerRadius=5;
borderView.layer.masksToBounds=YES;
UIView *topBorder = [[UIView alloc]init];
topBorder.backgroundColor = [customColor colorWithHexString:#"1e1a36"];
CGRect frameRect=textField.frame;
frameRect.size.height=CGRectGetHeight(textField.frame)/1.5;
topBorder.frame = frameRect;
frameRectEmail.size.width=frameRect.size.width;
[borderView setFrame:frameRectEmail];
[_textFieldBackGroundView addSubview:borderView];
[_textFieldBackGroundView addSubview:topBorder];
[_textFieldBackGroundView addSubview:textField];
}

Related

UILabelView cropping when expanded, but renders fully when inspected in view hierarchy

Image 1 shows issue. label has flexible width & height constrains, also we adding fix width & height to expand with. in View hierarchy it shows fully expanded as expected, but issue in real view:
Image 2 shows fully expanded label in View hierarchy:
Lable code
self.textLabel = [[RAVerticallyAlignedLabel alloc] initWithFrame: self.bounds andVerticalTextAlignment: [self verticalTextAlignment]];
self.textLabel.backgroundColor = ClearColour;
self.textLabel.userInteractionEnabled = NO;
self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.textLabel.numberOfLines = 0;
viewToTruncate = self.textLabel;
textWidth = MAX(MIN(screenSize.width - 20, [label.text sizeWithAttributes: #{NSFontAttributeName : label.font}].width + 10);
[UIView animateWithDuration: 0.3 animations: ^
{
self->containerView.frame = CGRectMake(xPos, top , MIN(textWidth, screenSize.width - 2 * RAPadding), newTextviewHeight);
// Container view is added inside scroll view
self->containerView.layer.shadowRadius = kRATruncationUtiltityShadowRadius;
self->containerView.layer.shadowOpacity = kRATruncationUtiltityShadowOpacity;
self->containerView.layer.shadowOffset = kRATruncationUtiltityShadowOffset;
self->containerView.layer.shadowPath = [UIBezierPath bezierPathWithRect: self->containerView.bounds].CGPath;
self->containerView.layer.masksToBounds = NO;
self->viewToExpand.frame = CGRectMake(0, 0, textWidth + componentContentWidth, self->containerView.height);
//viewToExpand is UILable, added inside container view
self->viewToExpand.layer.borderWidth = 1.0;
self->viewToExpand.layer.borderColor = borderColor.CGColor;
self->viewToExpand.layer.backgroundColor = backgroundColor.CGColor;
}];
Found issue fix, it size wasn't issue but "gradientMask" was playing up on expand as below:
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.frame = CGRectMake(0, 0, viewToTruncate.width, viewToTruncate.height);
gradientMask.colors = #[(id)WhiteColour.CGColor,(id)WhiteColour.CGColor, (id)ClearColour.CGColor];
gradientMask.locations = #[#0, #(locationOfFade), #1];
viewToTruncate.layer.mask = gradientMask;
removing mask on expand , it was rendering as expected.

Round button with border and shadow

What would be the best way to create a button such as the one in the image?
I tried:
self.button.layer.shadowColor = UIColor.redColor.CGColor;
self.button.layer.shadowRadius = 30.0f;
self.button.layer.shadowOpacity = 1.0f;
self.button.layer.shadowOffset = CGSizeMake(0.0f, 30.0f);
self.button.clipsToBounds = NO;
[self.button setNeedsDisplay];
But this just add a minor very blury shadow not at all like in the image.
Hey #user426132 set UIbutton background image and Try this code
For Objective-c
self.button.layer.cornerRadius = self.button.frame.size.width / 2;
self.button.layer.masksToBounds = false;
self.button.layer.shadowColor = UIColor.grayColor.CGColor;
self.button.layer.shadowOpacity = 1.0;
self.button.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.button.layer.shadowRadius = 10;
self.button.layer.shouldRasterize = true;
self.button.layer.rasterizationScale = UIScreen.mainScreen.scale;
For Swift 4.2
self.btnShadow.layer.cornerRadius = self.btnShadow.frame.size.width / 2
self.btnShadow.layer.masksToBounds = false
self.btnShadow.layer.shadowColor = UIColor.black.cgColor
self.btnShadow.layer.shadowOpacity = 1.0
self.btnShadow.layer.shadowOffset = CGSize(width: 0, height: 0)
self.btnShadow.layer.shadowRadius = 10
self.btnShadow.layer.shouldRasterize = true
self.btnShadow.layer.rasterizationScale = UIScreen.main.scale
Output is here in black cross is image

How to draw a 1px line in storyBoard?

As we know, the table view separator is so thin and beautiful. Sometimes we have to build some separator line in storyboard or nib, the min number is 1, but actually the line appears much thicker than we expected.
My question is how to draw a 1px line in storyboard?
Xcode 9 Swift: You can add a thin seperator with 1px line in the Storyboard using UIView.
Under Size Inspector, Just set the height to 1, width example to 360.
Apple documentation on UIView.
I got your point too, finally I find way out.
As we know, if we draw a line and set the height by code, we can set the height equal to (1.0 / [UIScreen mainScreen].scale).
But here, you wanna to draw in storyboard.
My way is subclass UIView or UIImageView, based on your demand as OnePXLine. In OnePXLine class, override layoutSubviews like below:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect rect = self.frame;
rect.size.height = (1 / [UIScreen mainScreen].scale);
self.frame = rect;
}
And you can draw 1 px line in storyboard by use this class.
Goodluck!
This would help you if you're coding in Swift:
func lineDraw(viewLi:UIView)
{
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0).CGColor
border.frame = CGRect(x: 0, y: viewLi.frame.size.height - width, width: viewLi.frame.size.width, height: viewLi.frame.size.height)
border.borderWidth = width
viewLi.layer.addSublayer(border)
viewLi.layer.masksToBounds = true
}
You can do like in any UIControl/UIElement for this and change the height as 1
If you want to 1 Point use this - 1
If you want to 1pixel Use this - 1.0 / UIScreen.mainScreen().scale
Objective-C
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 1)]; // customize the frame what u need
[lineView setBackgroundColor:[UIColor yellowColor]]; //customize the color
[self.view addSubview:lineView];
Swift
var lineView = UIView(frame: CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: 1))
lineView.backgroundColor = UIColor.yellow
self.view.addSubview(lineView)
If you want a more information see this once
you can assign float value for view frame . so what you can do is take any view (lable,textview,view,textfield) and assign height as 0.4f or something programatically and width to match self width.
lable.frame = CGRectMake(0,0,width of self.view,0.4f);
Try it in cellForRowAtIndex
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:242/255.0f green:222/255.0f blue:52/255.0f alpha:1.0];
[cell.contentView addSubview:separatorLineView];
Swift 5 code base on what #childrenOurFuture answered:
class OnePXLine: UIView {
override func layoutSubviews() {
super.layoutSubviews()
var rect = self.frame;
rect.size.height = (1 / UIScreen.main.scale);
self.frame = rect;
}
}

draw a circle of rectangles in Xcode

I have tried this code to draw a circle of rectangle
incNumberDefault=0;
for (int i = 0; i < maxNumber; i++) {
incNumberDefault = incNumberDefault +5;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(incNumberDefault, 100, 3, 15)];
myView.center = CGPointMake(130,130);
myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(incNumberDefault));
myView.backgroundColor = [UIColor lightGrayColor];
[self.speedDisplay addSubview:myView];
}
the outputs seems close circle where as i need transparent or space in middle of circle.
You can set UIView cornerRadius to half of the UIView width or height like,
myView.layer.cornerRadius=myview.frame.size.width / 2; or
myView.layer.cornerRadius=myview.frame.size.height / 2
For that you have to give same width and height to your UIView.
Try this :
// initialize the view with same height and width for perfect circle.
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 15, 15)];
// round the corner of view with required radius.
myView.layer.cornerRadius = myView.frame.size.width/2; // height can also be used
// circle with space in middle.
myView.backgroundColor = [UIColor clearColor];
myView.layer.borderColor = [UIColor lightGrayColor].CGColor;
myView.layer.borderWidth = 2.0f;
[self.speedDisplay addSubview:myView];
Hope it will solve your problem.
Thank you for your answer but i want to develop a dashed circle in certain distance and i have tired this code :
incNumberDefault=0;
for (int i = 0; i < maxNumber; i++) {
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100+60 * sin(incNumberDefault), 100+60*cos(incNumberDefault), 3, 15)];
//myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(incNumberDefault));
myView.backgroundColor = [UIColor lightGrayColor];
[self.speedDisplay addSubview:myView];
incNumberDefault = incNumberDefault +9;
}
but it generate random rectangle in a circle.
I want it in sequence.
Thank you.

CALayer inner border corner radius

I have a UITextView where I set the border width, border color, and corner radius properties on its layer, and the outside looks great. However, the inner part of the border doesn't have rounded corners like the outer part, and it looks kind of funny. Is there a way to round the inner corners of the border?
Edit:
Here's the code I used in the initWithFrame: method:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = UIColorFromRGB(0xdedede);
self.layer.cornerRadius = kTextFieldCornerRadius;
self.layer.borderColor = UIColorFromRGB(0xD4974C).CGColor;
self.layer.borderWidth = 3.0f;
self.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0f];
[self setClipsToBounds:YES];
[self.layer setMasksToBounds:YES];
}
return self;
}
And here's a screenshot of what it looks like now:
Notice the outer corners are rounded as expected, but the inner corners of the border are pointed rather than rounded. That's what I'm trying to fix. Thanks for your help!
Try to set this,
[txtView setClipsToBounds:YES]; //Confirms subviews are confined to the bounds of the view
[txtView.layer setMasksToBounds:YES]; //Confirms sublayers are clipped to the layer’s bounds
EDIT
Probably the value of kTextFieldCornerRadius is set to low in your case.
If I set kTextFieldCornerRadius = 7; see I can get perfect output.
try to increase your radius value.
Import QuartzCore framework and add the following lines of code :
OBJECTIVE - C
UIView *yourView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
yourView.layer.borderColor = [UIColor redColor].CGColor;
yourView.layer.borderWidth = 10.0f;
yourView.layer.cornerRadius = 20.0f;
[yourView setClipsToBounds:YES];
[yourView.layer setMasksToBounds:YES];
SWIFT - 3.0.1 (playground code)
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
import QuartzCore
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 100.0))
containerView.backgroundColor = UIColor.white
containerView.layer.borderWidth = 10
containerView.layer.borderColor = UIColor.red.cgColor
containerView.clipsToBounds = true
containerView.layer.masksToBounds = true
containerView.layer.cornerRadius = 20
PlaygroundPage.current.liveView = containerView
PlaygroundPage.current.needsIndefiniteExecution = true
OUTPUT :
IMPORTANT:
Make sure the cornerRadius is greater than the borderWidth . Else you will not be able to see the difference.

Resources