How to scroll to line in QML TextArea? - scroll

How can I scroll to a particular line in a multiline, rich text QML TextArea? There is positionToRectangle but that only accepts a position as an int which doesn't seem suitable for multiline text.

I suggest trying to calculate the position based on the line number multiplied by line height. You can calculate height of one line like this:
(textArea.implicitHeight - 2 * textArea.textMargin) / textArea.lineCount
This might help you to use positionToRectangle.
edit: as an afterthought, have you tried setting cursorPosition?

A TextArea is not scrollable by itself (cf Scrollable TextArea):
If you want to make a TextArea scrollable, [...] it can be placed inside a ScrollView.
Then you just have to set the contentY of your contentItem:
ScrollView {
id: view
width: parent.width
height: 60
TextArea {
text: "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL"
}
}
Button {
text: "scroll"
onClicked: view.contentItem.contentY += 10
}

Related

SwiftUI align SF Symbol as text with a text

I need to display a SF Symbol (image) and a text in a view (as a title). As the text could be quite long, I want that it passes under the image for second line.
Expected result (almost!):
I first tried a HStack with image and text. But text doesn't go under image: Second line starts with a left 'padding' (corresponding to left image space).
So I did this code (to get previous screenshot):
HStack(alignment:.top) {
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
+
Text("A long text as example")
.font(.title2).bold()
.foregroundColor(.pink)
}
.frame(width:140)
But I want to align image with center of first line. Center of image should be vertically in middle of top of A and bottom of g.
It is important that my second (and more) line passes under my image. And I don't want a bigger font for image.
I don’t know if you can force the image to automatically be centred in all cases, but you can add a .baselineOffset() modifier to the image-in-text to shift the image upwards by a fixed amount. For example:
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
.baselineOffset(3.0)
+
Text("A long text as example")
.font(.title2).bold()
.foregroundColor(.pink)
Once you have hit upon the right offset amount for the default text size, you might be able to accommodate accessibility variations by making the offset size relative to a base size, e.g.
Struct MyView: View {
#ScaledMetric(relativeTo: .title2) var baselineOffset: CGFloat = 3.0
// and in your body…
.baselineOffset(baselineOffset)
Firstly, you can remove the HStack because it is redundant. You can just replace it with brackets () to encapsulate the new Text, so you can use more modifiers like for setting the width.
The main solution is to use baselineOffset(_:) which means you can offset your image from the baseline of Text. Below, we only offset by 1, but this can be any value you want (including decimal numbers, e.g. 0.99).
Code:
struct ContentView: View {
var body: some View {
(
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
.baselineOffset(1)
+
Text("A long text as example")
.font(.title2)
.bold()
.foregroundColor(.pink)
)
.frame(width: 140)
}
}
Result:
Bonus: you can move your foregroundColor(.pink) modifier after the Texts have been combined, so you don't have to duplicate that.
Have you tried embedding Image in Text?
Text("Super star \(Image(systemName: "star"))")

Horizontal scrollView/UIImageView layout issue

The goal: Have a scroll view that displays an array of uiimageviews (photos) that you can horizontally scroll through them
How I understand to do this: Make the frame (CGRect) of each uiimageview the height and width of the scroll view, the y value to 0 on each, and set the first imgViews x value to 0. For every imgView after that, add the width of the scrollview to the x value. In theory, this would line the imgViews (Photos) up next to each other horizontally and not allow for any vertical scrolling or zooming, purely a horizontal photo viewer.
The storyboard setup: I am creating my scrollview in a xib file (It’s a custom uiCollectionViewCell), with these constraints:
— Top space to cell (0)
— Trailing space to cell (0)
— Leading space to cell (0)
— Height of 400
— Bottom space to a view (0)
—— (See Below for img)
Laying out the UIImgViews:
func layoutScrollView() {
for (index, img) in currentImages.enumerate() {
let imgView = UIImageView(frame: CGRect(x: CGFloat(index) * scrollView.bounds.width, y: CGFloat(0), width: scrollView.bounds.width, height: scrollView.bounds.height))
imgView.contentMode = .ScaleAspectFill
imgView.image = img
scrollView.addSubview(imgView)
scrollView.contentSize = CGSize(width: imgView.frame.width * CGFloat(index), height: scrollView.bounds.height)
scrollView.setNeedsLayout()
}
}
My suspicion: I suspect the issue is stemming from the auto layout constraints i’ve specified, but (considering Im asking a SO question) not sure
If there is a better way to do this (really the correct way) please let me know! I have been trying to wrap my head around this for a few days now.
I appreciate all responses! Thanks for reading
EDIT #1
I tried paulvs approach of setting setNeedsLayout & layoutIfNeeded before the "for" loop, and still no luck. Here is (out of three images selected) the second photo displaying. It seems that both the first and second photos are way longer than the content view and that would move the middle view over (Squished).
Your code looks fine except for a few details (that may be causing the problem):
Add:
view.setNeedsLayout()
view.layoutIfNeeded()
before accessing the scrollView's frame (a good place would be before the for-loop).
This is because when using Autolayout, if you access a view's frame before the layout engine has performed a pass, you will get incorrect frames sizes/positions.
Remove these lines from inside the for-loop:
scrollView.contentSize = CGSize(width: imgView.frame.width * CGFloat(index), height: scrollView.bounds.height)
scrollView.setNeedsLayout()
and place this line after (outside) the for loop:
scrollView.contentSize = CGSize(width: imgView.frame.width * CGFloat(currentImages.count), height: scrollView.bounds.height)

white space coming in place of scroll bar while filtering and long text is coming with

Here is the plunker created http://plnkr.co/edit/5DhDmI1Odhrys4jYDwIB?p=preview
I have associated textbox with ng-grid filter.
$scope.filterOptions = {
filterText:''
}
$scope.$watch('filterText',function(){
$scope.filterOptions.filterText=$scope.filterText;
});
If you enter "moroni" in the text box, only one row in grid will be displayed. But at the right, white space is visible. Is there a way to fix it.
First row in the plunker example is having very big string, When text is very long, only part of it is displayed. Is it possible to break the string and display it in multiple lines.
You can fix the text not wrapping issue by setting the rowHeight in gridoptions to value that fits your longest string:
rowHeight:50
And add this definition to your css:
.ngCellText {
white-space: unset;
}
The width whitespace issue is clearly a bug in ng-grid. This grid is not really a table but a lot of positioned and measured divs that look like a table. Seems the developers forgot to add some extra width to the row when no scrollbar is visible. You can only overcome this if you patch the code (not recommended) or setting the gridheight to a value in which all rows can be displayed without scrollbars.
.gridStyle {
border: 1px solid rgb(212,212,212);
width: 500px;
height: 300px
}
Look at this Plunker.
Anyhow, since these are mere unpractical hacks, I suggest you have a look at table based directive like trNgGrid which has all the features of ng-grid but is way more flexible when it comes to dynamic row heights.

How to increase vertical offset of tooltip position?

I'm using kendo tooltips on a graphic (within an anchor link) which is 24px tall. Accordingly, when the tooltip shows up (default position of bottom), it covers the bottom third of the graphic and so the bottom third of the graphic can't be clicked.
I can do the following:
.k-tooltip {
margin-top: 8px;
}
But the problem with this is that if the tooltip is on a graphic at the bottom of the page, the position will be "top" instead of "bottom" but it'll now be covering about 1/2 the graphic instead of just a third because it's still being pushed down by 8px.
What I'd like is if the position is bottom, then the margin-top is 8px, but if the position is top, the the margin-bottom is 8px.
Thanks for any help you can provide!
Billy McCafferty
Would this one help you?
http://dojo.telerik.com/amoZE/5
var tooltip = $("#demo").kendoTooltip({
filter: "a",
show: function (e) {
var position = e.sender.options.position;
if (position == "bottom") {
e.sender.popup.element.css("margin-top", "10px");
} else if(position == "top") {
e.sender.popup.element.css("margin-bottom", "10px");
}
}
}).data("kendoTooltip");
Thank you for your answer, jarno-lahtinen. It was very helpful!
Two problems came up with it and I would like to document the solutions here:
1. Property Error in Typescript
I am using TS and it gave me the following error:
"Property popup does not exist on type Tooltip" for e.sender.popup. I am not sure if this is due to a newer version of Kendo, or of missing type definitions.
Solution:
you can use this.popup instead.
2. Not working for position: "top"
Unfortunately, the "margin-bottom" has absolutely no effect because the popup is positioned "absolute" using top/left.
Solution:
this.popup.element.css("margin-top", "-10px");
This will shift the popup upwards by 10 pixels

QML: Simple word wrap with TextEdit element?

I'm just a beginner to QML and I wanted to make a simple example, which contains just one Rectangle with a TextEdit element:
import QtQuick 1.0
Rectangle {
width: 400; height: 400
color: "lightblue"
TextEdit {
x: 50; y: 100; width: 300; height: 300
text: "editable text editable text editable text editable text "
font.family: "Helvetica"; font.pixelSize: 32
}
}
The idea here is to just have a few lines displayed that the user can change or add. I just would like it to display it as multiple word-wrapped lines instead of just on a single line. I don't even need a scrollbar. Since TextEdit does not have a WrapMode property, how can I do this? :-(
Thanks!
Nina
TextEdit has a wordWrap property.
See http://doc.qt.nokia.com/4.7/qml-textedit.html#wrapMode-prop.
if you add
wrapMode: TextEdit.WordWrap
to the TextEdit component the text is wrapped to multiple lines.

Resources