简体   繁体   中英

How to have two different border stroke color for textfield-ios

In our app we wanted to have 2 different border colors around textfields (eg: top and left edge in 1 color, and bottom and right edge in another color) -Is this possible? Any help will be appreciated.

There's no pre-built way to have different colors for each border side, but you can fake it with layers . Probably more work than it's worth, but basically what you want to do is draw individual calayers for each border, then add them to the textfield.

#import <QuartzCore/QuartzCore.h> //This goes up top, but you already know that :-)

CALayer *topBorder = [CALayer layer];
topBorder.frame = // cgrect of where you want the bottom border. Use the textfields frame as reference, but treat the border as a solid rectangle
topBorder.backgroundColor = [UIColor greenColor].CGColor; // the .CGColor is important, don't forget it

[toScrollView.layer addSublayer:topBorder];

Then just rinse and repeat for each side. You can even set the dominant color normally, then just add the sides that differ (less work)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM